Title: | Report Errors in Web Applications with 'Problem Details' (RFC 7807) |
---|---|
Description: | Tools for emitting the 'Problem Details' structure defined in 'RFC' 7807 <https://tools.ietf.org/html/rfc7807> for reporting errors from 'HTTP' servers in a standard way. |
Authors: | Aaron Jacobs [aut, cre] |
Maintainer: | Aaron Jacobs <[email protected]> |
License: | Apache License (>= 2) |
Version: | 1.0.1.9000 |
Built: | 2024-11-17 02:52:59 UTC |
Source: | https://github.com/atheriel/httpproblems |
http_problem()
creates the "Problem Details" structure defined in
RFC 7807, used for reporting errors
from HTTP APIs in a standard way.
There are also helper methods for the most common HTTP problems: HTTP 400 Bad Request, 404 Not Found, 401 Unauthorized, 403 Forbidden, 409 Conflict, and 500 Internal Server Error.
http_problem( detail = NULL, status = 500L, type = NULL, title = NULL, instance = NULL, ... ) bad_request(detail = NULL, instance = NULL, ...) unauthorized(detail = NULL, instance = NULL, ...) forbidden(detail = NULL, instance = NULL, ...) not_found(detail = NULL, instance = NULL, ...) conflict(detail = NULL, instance = NULL, ...) internal_server_error(detail = NULL, instance = NULL, ...)
http_problem( detail = NULL, status = 500L, type = NULL, title = NULL, instance = NULL, ... ) bad_request(detail = NULL, instance = NULL, ...) unauthorized(detail = NULL, instance = NULL, ...) forbidden(detail = NULL, instance = NULL, ...) not_found(detail = NULL, instance = NULL, ...) conflict(detail = NULL, instance = NULL, ...) internal_server_error(detail = NULL, instance = NULL, ...)
detail |
A human-readable string giving more detail about the error, if possible. |
status |
The HTTP status code appropriate for the response. |
type |
A URL pointing to human-readable documentation for this type of
problem. When |
title |
A "short, human-readable summary of the problem type".
When |
instance |
A URL that identifies the specific occurrence of the
problem, if possible. When |
... |
Additional fields added to the problem as Extension Members. |
An object of class "http_problem"
, which has fields corresponding
to an RFC 7807 Problem Details structure.
stop_for_http_problem for issuing R errors with these structures.
body <- bad_request("Parameter 'id' must be a number.") str(body)
body <- bad_request("Parameter 'id' must be a number.") str(body)
Many APIs will not need to define custom problem "types", since HTTP status codes are usually illustrative enough. This function lists the default type and title information for a given status code.
http_problem_types()
http_problem_types()
A data frame of HTTP status codes and their default title & type.
The various stop_for_*()
functions leverage R's condition system to signal
an error with a custom type embedding the "Problem Details" structure
defined in RFC 7807.
They can be used for reporting errors from HTTP APIs in a standard way.
There are also helper methods for the most common HTTP problems: HTTP 400 Bad Request, 404 Not Found, 401 Unauthorized, 403 Forbidden, 409 Conflict, and 500 Internal Server Error.
stop_for_http_problem( detail = NULL, status = 500L, type = NULL, title = NULL, instance = NULL, ... ) stop_for_bad_request(detail = NULL, instance = NULL, ...) stop_for_unauthorized(detail = NULL, instance = NULL, ...) stop_for_forbidden(detail = NULL, instance = NULL, ...) stop_for_not_found(detail = NULL, instance = NULL, ...) stop_for_conflict(detail = NULL, instance = NULL, ...) stop_for_internal_server_error(detail = NULL, instance = NULL, ...)
stop_for_http_problem( detail = NULL, status = 500L, type = NULL, title = NULL, instance = NULL, ... ) stop_for_bad_request(detail = NULL, instance = NULL, ...) stop_for_unauthorized(detail = NULL, instance = NULL, ...) stop_for_forbidden(detail = NULL, instance = NULL, ...) stop_for_not_found(detail = NULL, instance = NULL, ...) stop_for_conflict(detail = NULL, instance = NULL, ...) stop_for_internal_server_error(detail = NULL, instance = NULL, ...)
detail |
A human-readable string giving more detail about the error, if possible. |
status |
The HTTP status code appropriate for the response. |
type |
A URL pointing to human-readable documentation for this type of
problem. When |
title |
A "short, human-readable summary of the problem type".
When |
instance |
A URL that identifies the specific occurrence of the
problem, if possible. When |
... |
Additional fields added to the problem as Extension Members. |
These functions call stop()
with a custom condition (with class
"http_problem_error"
), so they do not return a value.
http_problem for creating the structure directly.
tryCatch( stop_for_bad_request("Parameter 'id' must be a number."), error = function(e) { str(e) } )
tryCatch( stop_for_bad_request("Parameter 'id' must be a number."), error = function(e) { str(e) } )