Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return 400 Bad Content on json, xml, geojson, rds parsing errors. #840

Open
meztez opened this issue Nov 7, 2021 · 1 comment
Open

Return 400 Bad Content on json, xml, geojson, rds parsing errors. #840

meztez opened this issue Nov 7, 2021 · 1 comment
Milestone

Comments

@meztez
Copy link
Collaborator

meztez commented Nov 7, 2021

Plumber should return a 400 Bad Content when a parser can't parse content. Invalid json, xml, etc...

Likely fix with the generalized plumberError condition approach.

@mbuet2ner
Copy link

This would be a very welcomed feature 😊.

As of now, I use a somewhat hacky approach for JSON errors. Certainly not perfect but maybe this helps someone in the meantime...

error_handler <- function(req, res, err) {
  if (!inherits(err, "api_error") &&
      grepl("lexical error", err$message, fixed = TRUE)) {
    res$status <- 400
    body <- "Received malformed JSON."
    logger::log_error(
      'malformed_json "{err}"'
    )
  }
  else if (!inherits(err, "api_error")) {
    res$status <- 500
    body <- "Internal server error."
    logger::log_error(
      'internal_error "{err}"'
    )
  } else {
    res$status <- err$status
    body <- err$message
  }
  res$body <- list(message = body)
}

@schloerke schloerke added this to the v1.3.0 milestone Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants