Documentation ¶
Overview ¶
Package httperrors provides functions to construct errors from HTTP responses.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New constructs an error from a HTTP response.
The error returned by this function capture the stack trace, is tagged with the method, scheme, host, and path of the original request that this response was obtained from (taken from the Request field of the given response).
The type of the error is set to the status of the response, for example a 404 Not Found error will return an error of type "NotFound".
It also may carry three other high-level types, "Temporary", "Timeout", and "Throttled" which are deducted from the status of the response, for example a 408 Request Timeout status will construct an error of type "Timeout".
Note that the response body is left untouched, so the program still has to close it at some point.
Here's an example of common use of the function:
r, err := http.Get("http://localhost:1234/") if err != nil { return errors.Adapt(err) } defer r.Body.Close() if r.StatusCode >= 300 { return httperrors.New(r) } // ...
func Wrap ¶
Wrap is similar to New but takes an extra error argument so it can be used on the returned values from functions like http.Get or http.(*Client).Do.
The function considers all responses with status codes equal or greater than 300 to be errors and in this case it closes the response body and returns an error.
If err is not nil, the function returns the result of calling errors.Adapt with err as argument.
Here's an example of common use of the function:
r, err := httperrors.Wrap(http.Get("http://localhost:1234/")) if err != nil { return err }
Types ¶
This section is empty.