Skip to content

Commit fe83fa3

Browse files
refactor: Add Design.md to explain refactor
Signed-off-by: Anthony TREUILLIER <anthony.treuillier@scality.com>
1 parent 403a302 commit fe83fa3

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

DESIGN.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Introduction
2+
This library aims to simplify error handling in Go through the following key features
3+
* One unique function with argument for standard error and go-errors
4+
* Variadic options following the "Functional Options" pattern
5+
* An error code, obtained by concatenating identifiers defined at each calls of subfunctions, that allow tracing the root cause
6+
* Comply with error interface (Error() string)
7+
8+
# One unique function
9+
The signature of this unique function would be:
10+
```go
11+
func Wrap(error, errors.Opts...) error
12+
```
13+
Where error can be of type
14+
1. standard errors
15+
2. go-errors
16+
17+
## standard errors
18+
The provided error will be considered as a root error and stored as cause of a new go-errors one
19+
20+
## go-errors
21+
The provided error will be considered as a template for a new go-errors one
22+
23+
# Variadic Options
24+
The variadic options would be the following one
25+
26+
* `WithDetail`/`WithDetailf`: provide a message that details the error
27+
* `WithProperty`: provide a key/value pair for additional informations (filename, path, username, ... )
28+
* `WithIdentifier`: used to provide an identifer, it could be concatenated with other idenfier from previous calls from subfunctions (See example below for clarity)
29+
30+
Signature could be as follow
31+
32+
```go
33+
errors.WithDetail(string)
34+
errors.WithDetailf(string, ...any)
35+
errors.WithProperty(string, any)
36+
errors.WithIdentifier(int)
37+
```
38+
39+
# Identifier
40+
41+
Below an example of using `go-errors` with identifier.
42+
Also, find the expected error message
43+
44+
```go
45+
package main
46+
47+
import (
48+
"fmt"
49+
50+
errors "github.com/scality/go-errors"
51+
)
52+
53+
var ErrForbidden = errors.New("forbidden")
54+
55+
func main(){
56+
err := call1()
57+
fmt.Println(err)
58+
}
59+
60+
func call1() error {
61+
return errors.Wrap(
62+
call2(),
63+
errors.WithIdentifier(19),
64+
)
65+
}
66+
67+
func call2() error {
68+
return errors.Wrap(
69+
call3(),
70+
errors.WithDetail("missing required role"),
71+
errors.WithProperty("Role", "Reader"),
72+
errors.WithProperty("User", "john.doe"),
73+
errors.WithIdentifier(12),
74+
)
75+
}
76+
77+
func call3() error {
78+
// Something went wrong here
79+
return errors.Wrap(
80+
ErrForbidden,
81+
errors.WithIdentifer(2),
82+
errors.WithDetail("permission denied"),
83+
errors.WithProperty("File", "test.txt"),
84+
)
85+
}
86+
```
87+
88+
In the following situation, the program would return
89+
```
90+
forbidden (19-12-2): permission denied: missing required role: File='test.txt', User='john.doe', Role:'Reader', at=(func='main.call3', file='main.go', line='270')
91+
```

0 commit comments

Comments
 (0)