如何在管道工API中返回特定的错误代码?

nee*_*elp 3 r plumber

我们如何从管道工 R API 返回特定的错误代码?

我们希望返回带有消息的 400 状态代码,例如“功能 x 丢失。”。我们阅读了管道工文档,但无法弄清楚这一点。

如果我们使用stop()500 内部服务器错误,则返回且没有具体消息。

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

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

 # ... # else continue with prediction
}

Run Code Online (Sandbox Code Playgroud)

小智 7

过滤器有可能返回响应,您可以在此处查看文档。例如:

#* @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
    }

}
Run Code Online (Sandbox Code Playgroud)