-
Notifications
You must be signed in to change notification settings - Fork 2k
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
more-flexible framework for error-checking #277
Comments
Could you explain a bit what problem this solves? |
The standard message ( This way, the httr user need only supply a formatting function for the message (which may be particular to that API-call), but will not have to recreate the entire error-checking framework. Another way to do it may be to allow a custom message-function as an additional arg to |
I still don't get it. If you're writing an API wrapper, you're going to have to wrap many other things too. |
Annotating an assertion with a little description to enhance the error message is not uncommon. For example if you are writing a client for you kitchen utilities API: req <- GET("http://httpbin.org/status/418")
stop_for_status(req, "espresso machine authentication") That would result in:
This can make it just a little bit easier for the user to understand where things did go wrong. Not sure if this is what OP has in mind, and if httr is the best place for this. |
This is sort-of what I had in mind, but with the thought that the second arg to To bounce off of the espresso example: # this is the user's responsibility to provide
message_espresso <- function(x) {
# here, the user can dig all sorts of stuff out of the response - this is the standard message
status <- httr::status_code(x)
msg <- httr::http_status(status)$message
paste0("espresso machine: ", msg)
}
req <- GET("http://httpbin.org/status/418")
stop_for_status(req, message_espresso) The thought is for the user to be able to rely on the existing httr framework to throw the error/warning - and to be responsible only for providing a function to compose the message. To echo @jeroenooms, I don't know either if httr is the best place for this, but I do appreciate the discussion. |
FWIW I think this would be quite useful. I would use it right now if were there. I wish I could do something like this: stop_for_status(req, 403,
"Check that your Sheet is 'published to the web', not just 'public on the web'.") i.e. conveniently issue a customized message for a specific status code in a specific situation. Yes we can just do this while we are wrapping everything else but this would be quite nice. |
Have you seen the example in |
Discussion moved to #302. |
It would nice to be able to customize error messages, so I made an addon package: https://github.com/ijlyttle/httrplus to do this.
My goal was to make sure that this approach would work (it seems to), and then to propose it here.
If it is thought to be reasonable way to go, I can make a PR to this repo (of course, incorporating feedback as needed).
It may be useful to have some boilerplate message-functions - I include a couple proposals:
message_standard()
- just a rewrite of what is already inhttr
message_verbose()
- a first pass at a more-verbose message.Excerpt from README at https://github.com/ijlyttle/httrplus
The purpose is to propose a framework around
httr::stop_for_status()
andhttr::warn_for_status()
. The aim is for you to be able to more-easily customise messsages and error/warning-signalling behaviour when usinghttr
.The central function in this package is
check_for_status()
. Its arguments are:resp
- ahttr::response
objecttype
- one ofc("error", "warning", "message")
message_function
- a function that takesresp
as its first argument, returns message...
- additional arguments tomessage_function
The default
message_function
ismessage_standard()
(provided here), a function that replicates the messages returned byhttr::stop_for_status()
andhttr::warn_for_status()
Hence, the existing
httr
functions could be rewritten:The text was updated successfully, but these errors were encountered: