needRhelp
needRhelp

Reputation: 3148

How to return specific error code in plumber API?

How can we return a specific error code from plumber R API?

We would like to return a 400 status code with a message, e.g. "Feature x is missing.". We read the plumber documentation but could not figure this out.

If we use stop() a 500 Internal Server Error is returned with no specific message.

#' predict
#' @param x Input feature
#'
#' @get /predict

function(x) {
  if (is.na(x)) {
    # error code 400 should be returned
  }

 # ... # else continue with prediction
}

Upvotes: 3

Views: 3168

Answers (1)

h1427096
h1427096

Reputation: 293

It is possible for filters to return a response, you can check the doc here. For example:

#* @get /predict
function(res, req, x=NA){

    if (is.na(x)) {
        res$status <- 400  
        list(error = "Feature x is missing.")
    } else {
        # ... # else continue with prediction
    }

}

Upvotes: 7

Related Questions