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
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ return errors.New("now this is unfortunate")
10
10
Then being handled with a no-brainer:
11
11
```go
12
12
if err != nil {
13
-
returnnil, err
13
+
return err
14
14
}
15
15
```
16
16
@@ -27,14 +27,14 @@ It doesn't take long to find out that this is not often enough. There's little f
27
27
> Error: wrong argument value
28
28
29
29
An *errorx* library makes an approach to create a toolset that would help remedy this issue with these considerations in mind:
30
-
* No extra care should be required for an error to have all the required debug information; it is the opposite that may constitute a special case
30
+
* No extra care should be required for an error to have all the necessary debug information; it is the opposite that may constitute a special case
31
31
* There must be a way to distinguish one kind of error from another, as they may imply or require a different handling in user code
32
32
* Errors must be composable, and patterns like ```if err == io.EOF``` defeat that purpose, so they should be avoided
33
33
* Some context information may be added to the error along the way, and there must be a way to do so without altering the semantics of the error
34
34
* It must be easy to create an error, add some context to it, check for it
35
35
* A kind of error that requires a special treatment by the caller *is* a part of a public API; an excessive amount of such kinds is a code smell
36
36
37
-
As a result, the goal of the library is to provide a brief, expressive syntax for a conventional error handling and to discourage usage patterns that bring less value than harm.
37
+
As a result, the goal of the library is to provide a brief, expressive syntax for a conventional error handling and to discourage usage patterns that bring more harm than they're worth.
38
38
39
39
Error-related, negative codepath is typically less well tested, though of, and may confuse the reader more than its positive counterpart. Therefore, an error system could do well without too much of a flexibility and unpredictability
40
40
@@ -43,11 +43,11 @@ Error-related, negative codepath is typically less well tested, though of, and m
43
43
With *errorx*, the pattern above looks like this:
44
44
45
45
```go
46
-
returnnil, errorx.IllegalState.New("unfortunate")
46
+
return errorx.IllegalState.New("unfortunate")
47
47
```
48
48
```go
49
49
if err != nil {
50
-
returnnil, errorx.Decorate(err, "this could be so much better")
50
+
return errorx.Decorate(err, "this could be so much better")
51
51
}
52
52
```
53
53
```go
@@ -74,6 +74,7 @@ And this, frankly, may be quite enough. With a set of standard error types provi
74
74
75
75
If an error requires special treatment, it may be done like this:
76
76
```go
77
+
// MyError = MyNamespace.NewType("my_error")
77
78
if errorx.IsOfType(err, MyError) {
78
79
// handle
79
80
}
@@ -83,6 +84,7 @@ Note that it is never a good idea to inspect a message of an error. Type check,
83
84
84
85
An alternative is a mechanisms called **traits**:
85
86
```go
87
+
// thie first parameter is a name of new error type, the second is a reference to existing declared trait
@@ -99,7 +101,7 @@ Note that here a check is made against a trait, not a type, so any type with the
99
101
100
102
The example above introduced ```errorx.Decorate()```, a syntax used to add message as an error is passed along. This mechanism is highly non-intrusive: any properties an original error possessed, a result of a ```Decorate()``` will possess, too.
101
103
102
-
Sometimes, though, it is not the desired effect. A possibility to make a type check as a double edged one, and should be restricted as often as it is allowed. The bad way to do so would be to create a new error and to pass an ```Error()``` output as a message. Among other possible issues, this would either lose or duplicate the stack trace information.
104
+
Sometimes, though, it is not the desired effect. A possibility to make a type check is a double edged one, and should be restricted as often as it is allowed. The bad way to do so would be to create a new error and to pass an ```Error()``` output as a message. Among other possible issues, this would either lose or duplicate the stack trace information.
103
105
104
106
A better alternative is:
105
107
```go
@@ -114,7 +116,7 @@ See ```WrapMany()``` and ```DecorateMany()``` for more sophisticated cases.
114
116
115
117
As an essential part of debug information, stack traces are included in all *errorx* errors by default.
116
118
117
-
When an error is passed along, the original stack trace is simply retained, as this typically takes place along the lines if the same frames that were originally captured. When an error is received from another goroutine, use this to add frames that would otherwise be missing:
119
+
When an error is passed along, the original stack trace is simply retained, as this typically takes place along the lines of the same frames that were originally captured. When an error is received from another goroutine, use this to add frames that would otherwise be missing:
0 commit comments