languageserver icon indicating copy to clipboard operation
languageserver copied to clipboard

error handling json: Error: lexical error: invalid char in json text.

Open Fred-Wu opened this issue 4 years ago • 8 comments

When running some of my R scripts, I always get following errors which shuts down the server. Cannot figure out what json text it is referring to, and what causes it.

[2021-08-09 16:03:38.041] error handling json:  Error: lexical error: invalid char in json text.
                                       Content-Length: 10492    {"json
                     (right here) ------^

Stack trace:
1: parse_string(txt, bigint_as_char)
2: parseJSON(txt, bigint_as_char)
3: parse_and_simplify(txt = txt, simplifyVector = simplifyVector, 
    simplifyDataFrame = simplifyDataFrame, simplifyMatrix = simplifyMatrix, 
    flatten = flatten, ...)
4: jsonlite::fromJSON(data, simplifyVector = FALSE)

[2021-08-09 16:03:38.046] Error: Unexpected non-empty line
Call: self$read_header()
Stack trace:
1: stop("Unexpected non-empty line")
2: self$read_header()
3: self$fetch(blocking = FALSE)

[2021-08-09 16:03:38.046] exiting

[Error - 4:03:38 pm] Connection to server is erroring. Shutting down server.
[Error - 4:03:38 pm] Connection to server is erroring. Shutting down server.
[Error - 4:03:38 pm] Connection to server is erroring. Shutting down server.

Fred-Wu avatar Aug 09 '21 07:08 Fred-Wu

What version of languageserver are you using? The latest version should fix a related issue if your file contains non ascii chars.

randy3k avatar Aug 09 '21 08:08 randy3k

What version of languageserver are you using? The latest version should fix a related issue if your file contains non ascii chars.

Version 0.3.11 Is this the latest one or should I install the development version?

Fred-Wu avatar Aug 09 '21 10:08 Fred-Wu

Well it is the latest version. Do your files or file names contain non ascii chars?

randy3k avatar Aug 09 '21 16:08 randy3k

Well it is the latest version. Do your files or file names contain non ascii chars?

I tried to copy and paste the codes lines by lines to see where it breaks. For one of my files, the server immediately shutdown after I copied and pasted the following codes at Line 194 to 198.

# create dataframe for model testing
df <- data.frame(Y, E, T1, T2, S1, S2, Vu, Vw)

# define priors
precision.prior <- list(prec = list(prior = "pc.prec", param = c(0.5, 0.01)))

Is there anything wrong with this?

For a second file, the intial open has nothing wrong with the server. However, once I started to type in it, it immediately shut down just with

a + 1.

This file has 246 lines.

Would the error just be caused by the number of lines in scripts?

Fred-Wu avatar Aug 16 '21 11:08 Fred-Wu

It appears that the error is quite random. One of my scripts was working completely fine, but today after opening it, and typing something, then the error occur and shut down the server.

Fred-Wu avatar Aug 18 '21 04:08 Fred-Wu

Do the names of the files or directories contain non ascii chars?

randy3k avatar Aug 18 '21 04:08 randy3k

Do the names of the files or directories contain non ascii chars?

Nothing looks wrong to me

C:\MHLL\self-harm\R\Youth_trend_paper.R

Fred-Wu avatar Aug 18 '21 04:08 Fred-Wu

I have the same issue, completely have no idea why I once tried to open debug = true in the lsp settings, and in the last there is a mode detailed debug log

My R --version is:

R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin20 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

I have take a look at my lsp.log and one of my typical error is: I have the followng error:

[2022-05-09 04:04:40.036] error handling json:  Error: lexical error: invalid char in json text.
            ys <- runif(n) >\n     {"params":{"textDocument":{"versi
                     (right here) ------^

Stack trace:
1: parse_string(txt, bigint_as_char)
2: parseJSON(txt, bigint_as_char)
3: parse_and_simplify(txt = txt, simplifyVector = simplifyVector, 
    simplifyDataFrame = simplifyDataFrame, simplifyMatrix = simplifyMatrix, 
    flatten = flatten, ...)
4: jsonlite::fromJSON(data, simplifyVector = FALSE)
[2022-05-09 04:04:40.039] Error: Unexpected non-empty line
Call: self$read_header()
Stack trace:
1: stop("Unexpected non-empty line")
2: self$read_header()
3: self$fetch(blocking = FALSE)
[2022-05-09 04:04:40.039] exiting

The code that yields the error is at here:

generation <- function(n, gamma0, beta0, gamma1, beta1, alpha) {
    `%>%` <- tidyverse::`%>%` # pass lintr object-usage linter
    ys <- runif(n) %>%
        (function(x) ifelse(x > alpha, 1, 0))
    ps <- runif(n)

    xs <- rep(0, n)

    xs[ys == 0] <- (log(ps[ys == 0] / (1 - ps[ys == 0])) + beta0) / gamma0
    xs[ys == 1] <- (log(ps[ys == 1] / (1 - ps[ys == 1])) + beta1) / gamma1

    ys <- ys[xs >= 0]
    xs <- xs[xs >= 0]

    data.frame(x = xs, y = factor(ys))
}

update: the related detailed debugged log is:

[2022-05-10 02:02:34.348] received:  Content-Length: 11817
[2022-05-10 02:02:34.374] error handling json:  Error: lexical error: invalid char in json text.
          [ys == 0] <- (log(ps[ys == {"params":{"contentChanges":[{"te
                     (right here) ------^

Stack trace:
1: parse_string(txt, bigint_as_char)
2: parseJSON(txt, bigint_as_char)
3: parse_and_simplify(txt = txt, simplifyVector = simplifyVector, 
    simplifyDataFrame = simplifyDataFrame, simplifyMatrix = simplifyMatrix, 
    flatten = flatten, ...)
4: jsonlite::fromJSON(data, simplifyVector = FALSE)
[2022-05-10 02:02:34.376] received:  0] \/ (1 - ps[ys == 0])) + beta0) \/ gamma0\n    xs[ys == 1] <- (log(ps[ys == 1] \/ (1 - ps[ys == 1])) + beta1) \/ gamma1\n\n    ys <- ys[xs >= 0]\n    xs <- xs[xs >= 0]\n\n    data.frame(x = xs, y = factor(ys))\n}\n\n# %%\nnewdata <- generation(914, 1.216, 4.576, 1.068, 5.284, alpha)\nnewdata$y <- factor(newdata$y)\n\ny0idx <- which(newdata$y == 0)\ny1idx <- which(newdata$y == 1)\n\ny0idx <- sample(y0idx, length(y0idx \/ 2), TRUE)\ny1idx <- sample(y1idx, length(y1idx) \/ 2, TRUE)\n\nnewdata <- newdata[c(y0idx, y1idx), ]\n\nobsy0idx <- which(age_binary == 0)\nobsy1idx <- which(age_binary == 1)\n\nobsy0idx <- sample(obsy0idx, length(obsy0idx \/ 2), TRUE)\nobsy1idx <- sample(obsy1idx, length(obsy1idx \/ 2), TRUE)\n\nobsy0 <- logy[obsy0idx]\nobsy1 <- logy[obsy1idx]\n\nnewdata <- rbind(\n    newdata,\n    data.frame(x = obsy0, y = 0),\n    data.frame(x = obsy1, y = 1)\n)\n# %%\n# %%\n\nnewdata$dist <- \"generated\"\nobsdata <- data.frame(x = logy, y = age_binary |> factor(), dist = \"observed\")\n# %%\n\ngeneratedvsobserved <- cowplot::plot_grid(\n    ggplot() +\n        geom_histogram(aes(x = x, y = ..density.., fill = \"generated\"),\n            alpha = 0.3,\n            data = newdata |> filter(y == 0)\n        ) +\n        geom_histogram(aes(x = x, y = ..density.., fill = \"observed\"),\n            alpha = 0.3,\n            data = obsdata |> filter(y == 0)\n        ) +\n        labs(title = \"y = 0\") +\n        theme_bw(),\n    ggplot() +\n        geom_histogram(aes(x = x, y = ..density.., fill = \"generated\"),\n            alpha = 0.3,\n            data = newdata |> filter(y == 1)\n        ) +\n        geom_histogram(aes(x = x, y = ..density.., fill = \"observed\"),\n            alpha = 0.3,\n            data = obsdata |> filter(y == 1)\n        ) +\n        labs(title = \"y = 1\") +\n        theme_bw()\n)\ngeneratedvsobserved\nggsave(\"generatedvsobserved.png\", generatedvsobserved, width = 10, height = 4)\n\n# %%\n# implement a simple linear regression\n\nlinearRegressoin <- function(x, y) {\n    (t(x) %*% x) |>\n        solve() %*% t(x) %*% y\n}\n\nonehotEncoding <- function(x) {\n    newx <- matrix(0, nrow = nrow(x))\n    newxColNames <- c(\"\")\n    for (col in seq_len(x)) {\n        uniqueVals <- unique(x[, col])\n        for (val in uniqueVals) {\n            newxColNames <- c(newxColNames, paste0(colnames(x)[col], \"_\", val))\n            newcol <- ifelse(x[, col] == val, 1, 0)\n            newx <- cbind(newx, newcol)\n        }\n    }\n    newx\n}\n# %%\n\nx <- data.frame(\n    a = c(rep(\"dog\", 50), rep(\"cat\", 50)),\n    b = c(rep(\"cat\", 50), rep)\n    )\n\n# %%\n# %%\n# ypred <- ifelse(ypred > 0.5, \"65+ years\", \"<65 years\")\n\n# %%\n# ysim = sigmoid(seq(1, 10, length = 1000), 5, 3)\n# coefsim = grad_desc(0.05, 100000, ysim, seq(1, 10, length = 1000))\n# coefsim\n#\n# %%\n# %%\n#  [1] \"case_month\"                      \"res_state\"                       \"state_fips_code\"                 \"res_county\"\n#  [5] \"county_fips_code\"                \"age_group\"                       \"sex\"                             \"race\"\n#  [9] \"ethnicity\"                       \"case_positive_specimen_interval\" \"case_onset_interval\"             \"process\"\n# [13] \"exposure_yn\"                     \"current_status\"                  \"symptom_status\"                  \"hosp_yn\"\n# [17] \"icu_yn\"                          \"death_yn\"                        \"underlying_conditions_yn\"\n"}],"textDocument":{"uri":"file:\/\/\/Users\/northyear\/Desktop\/hw2022\/DataMining\/finalreport\/final13.R","version":29}},"method":"textDocument\/didChange","jsonrpc":"2.0"}Content-Length: 220
[2022-05-10 02:02:34.376] Error: Unexpected non-empty line
Call: self$read_header()
Stack trace:
1: stop("Unexpected non-empty line")
2: self$read_header()
3: self$fetch(blocking = FALSE)
[2022-05-10 02:02:34.376] exiting

milanglacier avatar May 09 '22 08:05 milanglacier