Skip to content
Snippets Groups Projects
Commit 1a9fcd62 authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Updated readme with HTTP info

parent 90267f8d
No related branches found
No related tags found
No related merge requests found
...@@ -59,6 +59,30 @@ if err := db.Select(query); err != nil { ...@@ -59,6 +59,30 @@ if err := db.Select(query); err != nil {
} }
``` ```
## func HTTP()
Create an HTTP error, which is the same as others but includes and HTTP Status code:
```
if err := db.Select(query); err != nil {
return errors.HTTP(http.StatusNotFound, err, "cannot read users")
}
```
To retrieve the code, use err.Code() which returns integer value.
If you wrapped an HTTP error in another HTTP error, there may be different codes in the stack.
The Code() method will dig down all the causes and return the lowest non-zero code value it finds.
So StatusNotFound (404) will have precedence over StatusInsufficientStorage (507)
It will return 0 when there are no codes in the error stack.
An error with HTTP code will also print the code in the stack, e.g.:
```
http-error_test.go:27: failed:
http-error_test.go(24): TestHTTPError() terrible mistake HTTP(404:Not Found), because
http-error_test.go(21): TestHTTPError() jissis this is bad! HTTP(507:Insufficient Storage), because
http-error_test.go(18): TestHTTPError() failed to get user HTTP(400:Bad Request), because
http-error_test.go(14): TestHTTPError() failed to connect to db
```
## Refactoring exiting code ## Refactoring exiting code
Replace all other errors package imports with this package: Replace all other errors package imports with this package:
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment