You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-1Lines changed: 28 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@ Errors package is a drop-in replacement of the built-in Go errors package with n
13
13
1. Multiple (11) error types
14
14
2. User friendly message
15
15
3. File & line number prefixed to errors
16
+
4. HTTP status code and user friendly message (wrapped messages are concatenated) for all error types
16
17
17
18
In case of nested errors, the messages (in case of nesting with this package's error) & errors are also looped through.
18
19
@@ -75,6 +76,11 @@ func main() {
75
76
76
77
A common annoyance with Go errors which most people are aware of is, figuring out the origin of the error, especially when there are nested function calls. Ever since error annotation was introduced in Go, a lot of people have tried using it to trace out an errors origin by giving function names, contextual message etc in it. e.g. `fmt.Errorf("database query returned error %w", err)`. This errors package, whenever you call the Go error interface's `Error() string` function, it'll print the error prefixed by the filepath and line number. It'd look like `../Users/JohnDoe/apps/main.go:50 hello world` where 'hello world' is the error message.
77
78
79
+
### HTTP status code & message
80
+
81
+
The function `errors.HTTPStatusCodeMessage(error) (int, string, bool)` returns the HTTP status code, message, and a boolean value. The boolean if true, means the error is of type *Error from this package.
82
+
If error is nested with multiple errors, it loops through all the levels and returns a single concatenated message. This is illustrated in the 'How to use?' section
83
+
78
84
## How to use?
79
85
80
86
Before that, over the years I have tried error with stack trace, annotation, custom error package with error codes etc. Finally, I think this package gives the best of all worlds, for most generic usecases.
@@ -169,10 +175,31 @@ And output of the `fmt.Println(err.Error())`
169
175
```bash
170
176
/Users/username/go/src/errorscheck/main.go:28 /Users/username/go/src/errorscheck/main.go:20 sinking bar
171
177
```
178
+
## Benchmark
179
+
180
+
Benchmark run on:
181
+
<p><imgwidth="320"alt="Screenshot 2020-07-18 at 6 25 22 PM"src="https://user-images.githubusercontent.com/1092882/87852981-241b5c80-c924-11ea-9d22-296acdead7cc.png"></p>
0 commit comments