Skip to content

Commit 69670bf

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

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

DESIGN.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
func call1() error {
60+
return errors.Wrap(
61+
call2(),
62+
errors.WithIdentifier(19),
63+
)
64+
}
65+
66+
func call2() error {
67+
return errors.Wrap(
68+
call3(),
69+
errors.WithDetail("missing required role"),
70+
errors.WithProperty("Role", "Reader"),
71+
errors.WithProperty("User", "john.doe"),
72+
errors.WithIdentifier(12),
73+
)
74+
}
75+
76+
func call3() error {
77+
// Something went wrong here
78+
return errors.Wrap(
79+
ErrForbidden,
80+
errors.WithIdentifer(2),
81+
errors.WithDetail("permission denied"),
82+
errors.WithProperty("File", "test.txt"),
83+
)
84+
}
85+
```
86+
87+
In the following situation, the program would return
88+
```
89+
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')
90+
```

0 commit comments

Comments
 (0)