Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
1a9fcd62
Commit
1a9fcd62
authored
3 years ago
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Updated readme with HTTP info
parent
90267f8d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
errors/README.md
+24
-0
24 additions, 0 deletions
errors/README.md
with
24 additions
and
0 deletions
errors/README.md
+
24
−
0
View file @
1a9fcd62
...
@@ -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:
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment