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

Plumber $run method needs documentation #449

Closed
rafamerlin opened this issue Jun 11, 2019 · 7 comments
Closed

Plumber $run method needs documentation #449

rafamerlin opened this issue Jun 11, 2019 · 7 comments
Labels
docs Related to documentation only

Comments

@rafamerlin
Copy link

rafamerlin commented Jun 11, 2019

Hi, I'm using the latest plumber docker image and trying to run a very simple script.

This is my dockerfile:

FROM trestletech/plumber

WORKDIR /app/

COPY . .

CMD ["/app/test.R"]

This is my test.R file:

#' @get /simple
function(){
  stop("I'm an error!")
}

#' @get /healthCheck
function(){
    list(200, "Health Check passed", Sys.getenv("hostname"))
}

Healthcheck returns a 200 and this:

[
    [
        200
    ],
    [
        "Health Check passed"
    ],
    [
        ""
    ]
]

However, if I call the error one, instead of returning the message as the documentation says it does, it just returns a 500 and this:

{
    "error": [
        "500 - Internal server error"
    ]
}

Am I missing some config or something? Maybe :latest is not the right tag for the docker image?

Ah, this is the plumber console:

R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> pr <- plumber::plumb(commandArgs()[4]); pr$run(host='0.0.0.0', port=8000)
Starting server to listen on port 8000
Warning in if (stri_startswith_fixed(body, "{")) { :
  the condition has length > 1 and only the first element will be used
<simpleError in (function () {    stop("I'm an error!")})(): I'm an error!>
@schloerke schloerke added help wanted Solution is well-specified enough that any community member could fix type: question Poses a question, not a problem and removed help wanted Solution is well-specified enough that any community member could fix labels Jun 11, 2019
@schloerke
Copy link
Collaborator

schloerke commented Jun 11, 2019

Correct. There should be a message key in the error object. It should be returning :

{
  "error": [ "500 - Internal server error" ],
  "message": ["Error in (function () : I'm an error!\n"]
}

If you run it in the console (not in docker), can you get the error message? If I copy your code and run the API, I can not reproduce it with CRAN or GitHub master branch.

This is odd.

@schloerke schloerke added the need info Further information has been requested label Jun 11, 2019
@rafamerlin
Copy link
Author

rafamerlin commented Jun 12, 2019

Hey @schloerke, thanks for answering.

I've installed plumber from Cran, and ran it like this:

Plumber.R

#' @get /simple
function(){
  stop("I'm an error!")
}

#' @get /healthCheck
function(){
  list(200, "Health Check passed", Sys.getenv("hostname"))
}

Saved it and ran in R Studio this code:

library(plumber)
r <- plumb("plumber.R")  # Where 'plumber.R' is the location of the file shown above
r$run(port=8000)

One thing I saw immediately is that the CRAN version runs swagger UI when running plumber. If I try /__swagger__/ in the one running on docker it will return me this:

{"error":["404 - Resource Not Found"]}

Also, when running the query for the error, it does return the message properly:

{
    "error": [
        "500 - Internal server error"
    ],
    "message": [
        "Error in (function () : I'm an error!\n"
    ]
}

I'm wondering why that would happen though, is there a possibility that the docker images on dockerhub are outdated? Maybe the latest tag is not the proper one. Maybe it's just missing some configuration that is automatically disabled for the docker images?

@schloerke
Copy link
Collaborator

When running in Docker, set r$run(port = 8000, swagger = TRUE). By default swagger is set to interactive(), which is FALSE in a Docker script.

@rafamerlin
Copy link
Author

rafamerlin commented Jun 13, 2019

Do you think that could be related to the error message not coming from docker? Were you able to test it on docker?

If you want I can create a project with it working so you can just clone and run to test it.

@rafamerlin
Copy link
Author

Hello again.

I managed to figure out why it wasn't printing the message. It was the debug interactive (your tip about swagger helped me to find it).

If you're not running plumber with Debug it will not print the message out for you. There's even a test for it: https://github.com/trestletech/plumber/blob/095b5377812d60664e6cc955b1417c4cf46d44c2/tests/testthat/test-default-handlers.R#L28

I've created my own docker image and added the debug option and now I get the error messages. I am not sure where to edit the docs from rplumber.io but maybe it would be useful to mention that there.

Thanks again for all the help.

@schloerke schloerke changed the title Stop not returning the message. Plumber $run method needs documentation Jun 18, 2019
@schloerke schloerke added docs Related to documentation only and removed need info Further information has been requested type: question Poses a question, not a problem labels Jun 18, 2019
@schloerke schloerke reopened this Jun 18, 2019
@rafamerlin
Copy link
Author

rafamerlin commented Jul 16, 2019

So, I've just found out that if I add a serializer to the method it will also not return the reason for stopping.

So, by adding this to my method I will not get the error back any more.

#' @serializer contentType list(type="application/json")

Just a workaround, if you remove the annotation on the serializer and do this instead,
it will work.

  res$setHeader("Content-Type", "application/json")
  res$body <- jsonlite::toJSON(val)

  return(res)

@schloerke
Copy link
Collaborator

R6 docs added in #530. Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Related to documentation only
Projects
No open projects
Magrittr Style
Awaiting triage
Development

No branches or pull requests

2 participants