Skip to content

Commit 5034d24

Browse files
committed
[-] fixed playground link and other minor changes
1 parent 90664f5 commit 5034d24

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,24 @@
99

1010
# Errors v1.3.1
1111

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,
1313
which should handle most of the use cases. Some of them are a bit too specific for web applications, but useful nonetheless.
1414

1515
Features of this package:
1616

17-
1. Multiple (11) error types
17+
1. Multiple (14) error types
1818
2. Easy handling of User friendly message(s)
1919
3. Stacktrace - formatted, unfromatted, custom format (refer tests in errors_test.go)
2020
4. Retrieve the Program Counters for the stacktrace
2121
5. Retrieve runtime.Frames using `errors.RuntimeFrames(err error)` for the stacktrace
2222
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
2627

2728
In case of nested errors, the messages & errors are also looped through the full chain of errors.
2829

29-
### Prerequisites
30-
31-
Go 1.13+
32-
3330
### Available error types
3431

3532
1. TypeInternal - For internal system error. e.g. Database errors
@@ -47,18 +44,18 @@ Go 1.13+
4744
13. TypeContextTimedout - For when the Go context has timed out
4845
14. TypeContextCancelled - For when the Go context has been cancelled
4946

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.
5249

5350
All the dedicated error type functions are documented [here](https://pkg.go.dev/github.com/naughtygopher/errors?tab=doc#DownstreamDependencyTimedout).
5451
Names are consistent with the error type, e.g. errors.Internal(string) and errors.InternalErr(error, string)
5552

5653
### User friendly messages
5754

5855
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.
6057

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
6259
are helper functions with the _suffix_ **'Err'**. All such helper functions accept the original error and a string.
6360

6461
```golang
@@ -118,19 +115,21 @@ formatted s: bar is not happy
118115
msg: bar is not happy
119116
```
120117

121-
[Playground link](https://go.dev/play/p/-WzDH46f_U5)
118+
[Playground link](https://go.dev/play/p/OiLegJ9Xxc9)
122119

123120
### File & line number prefixed to errors
124121

125-
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.
126125

127-
### HTTP status code & message
126+
### HTTP/GRPC status code & message
128127

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.
130129

131130
## How to use?
132131

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.
134133

135134
```golang
136135
package main

0 commit comments

Comments
 (0)