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
+17-18Lines changed: 17 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,27 +9,24 @@
9
9
10
10
# Errors v1.3.1
11
11
12
-
Errors package is a drop-in replacement of the built-in Go errors package. It lets you create errors of 11 different types,
12
+
Errors package is a drop-in replacement of the built-in Go errors package. It lets you create errors of 14 different types,
13
13
which should handle most of the use cases. Some of them are a bit too specific for web applications, but useful nonetheless.
14
14
15
15
Features of this package:
16
16
17
-
1. Multiple (11) error types
17
+
1. Multiple (14) error types
18
18
2. Easy handling of User friendly message(s)
19
19
3. Stacktrace - formatted, unfromatted, custom format (refer tests in errors_test.go)
20
20
4. Retrieve the Program Counters for the stacktrace
21
21
5. Retrieve runtime.Frames using `errors.RuntimeFrames(err error)` for the stacktrace
22
22
6. HTTP status code and user friendly message (wrapped messages are concatenated) for all error types
23
-
7. Helper functions to generate each error type
24
-
8. Helper function to get error Type, error type as int, check if error type is wrapped anywhere in chain
25
-
9.`fmt.Formatter` support
23
+
7. GRPC status code and user friendly message (wrapped messages are concatenated) for all error types
24
+
8. Helper functions to generate each error type
25
+
9. Helper function to get error Type, error type as int, check if error type is wrapped anywhere in chain
26
+
10. . `fmt.Formatter` support
26
27
27
28
In case of nested errors, the messages & errors are also looped through the full chain of errors.
28
29
29
-
### Prerequisites
30
-
31
-
Go 1.13+
32
-
33
30
### Available error types
34
31
35
32
1. TypeInternal - For internal system error. e.g. Database errors
@@ -47,18 +44,18 @@ Go 1.13+
47
44
13. TypeContextTimedout - For when the Go context has timed out
48
45
14. TypeContextCancelled - For when the Go context has been cancelled
49
46
50
-
Helper functions are available for all the error types. Each of them have 2 helper functions, one which accepts only a string,
51
-
and the other which accepts an original error as well as a user friendly message.
47
+
Helper functions are available for all the error types. Each of them have 3 helper functions, one which accepts only a string,
48
+
another which accepts an original error as well as a user friendly message, and one which accepts format string along with arguments.
52
49
53
50
All the dedicated error type functions are documented [here](https://pkg.go.dev/github.com/naughtygopher/errors?tab=doc#DownstreamDependencyTimedout).
54
51
Names are consistent with the error type, e.g. errors.Internal(string) and errors.InternalErr(error, string)
55
52
56
53
### User friendly messages
57
54
58
55
More often than not when writing APIs, we'd want to respond with an easier to undersand user friendly message.
59
-
Instead of returning the raw error and log the raw error.
56
+
Instead of returning the raw error, just log the raw error.
60
57
61
-
There are helper functions for all the error types. When in need of setting a friendly message, there
58
+
There are helper functions for all the error types. When in need of setting a friendly message for an existing error, there
62
59
are helper functions with the _suffix_**'Err'**. All such helper functions accept the original error and a string.
63
60
64
61
```golang
@@ -118,19 +115,21 @@ formatted s: bar is not happy
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)`. However this errors package, whenever you call the Go error interface's `Error() string` function, prints 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.
122
+
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.
123
+
Annotations help a lot by being able to provide contextual message to errors. e.g. `fmt.Errorf("database query returned error %w", err)`.
124
+
However in this package, the `Error() string` function (Go error interface method), prints 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.
126
125
127
-
### HTTP status code & message
126
+
### HTTP/GRPC status code & message
128
127
129
-
The function`errors.HTTPStatusCodeMessage(error) (int, string, bool)` returns the HTTP status code, message, and a boolean value. The boolean is true, if the error is of type \*Error from this package. If error is nested, it unwraps and returns a single concatenated message. Sample described in the 'How to use?' section
128
+
The functions`errors.HTTPStatusCodeMessage(error) (int, string, bool), errors.GRPCStatusCodeMessage(error) (int, string, bool)` returns the HTTP/GRPC status code, message, and a boolean value. The boolean is true, if the error is of type \*Error from this package. If error is nested, it unwraps and returns a single concatenated message. Sample described in the 'How to use?' section.
130
129
131
130
## How to use?
132
131
133
-
A sample was already shown in the user friendly message section, following one would show a few more scenarios.
132
+
Other than the functions explained earlier in the _**User friendly messages**_ section, more examples are provided below.
0 commit comments