@@ -28,7 +28,8 @@ import (
2828)
2929
3030func main () {
31- err := errors.New (" database error" ).
31+ var ErrDB = errors.New (" database error" )
32+ err := errors.From (ErrDB).
3233 WithIdentifier (1001 ).
3334 WithDetail (" connection timeout" ).
3435 WithProperty (" host" , " localhost" ).
@@ -40,45 +41,40 @@ func main() {
4041
4142## Usage Examples
4243
43- ### Creating Basic Errors
44-
45- ``` go
46- // Simple error
47- err := errors.New (" validation failed" ).Throw ()
48-
49- // Error with identifier
50- err := errors.New (" not found" ).
51- WithIdentifier (404 ).
52- Throw ()
53- ```
54-
5544### Adding Details and Properties
5645
5746``` go
47+ // Initializing Domain errors
48+ var (
49+ ErrValidationFailed = errors.New (" validation failed" )
50+ ErrRequestFailed = errors.New (" request failed" )
51+ ErrDatabaseError = errors.New (" database error" )
52+ )
53+
5854// Multiple details
5955// Each WithDetail() call appends to the Details slice
60- err := errors.New ( " validation failed " ).
56+ err1 := errors.From (ErrValidationFailed ).
6157 WithDetail (" email is required" ).
6258 WithDetail (" password must be at least 8 characters" ).
6359 Throw ()
6460
6561// Details can also be formatted
66- err := errors.New ( " request failed " ).
62+ err2 := errors.From (ErrRequestFailed ).
6763 WithDetailf (" failed to connect to %s :%d " , " api.example.com" , 443 ).
6864 WithDetail (" timeout after 30 seconds" ).
6965 Throw ()
7066
7167// Single property
72- err := errors.New ( " request failed " ).
68+ err3 := errors.From (ErrRequestFailed ).
7369 WithProperty (" url" , " https://api.example.com" ).
7470 WithProperty (" status_code" , 500 ).
7571 Throw ()
7672
7773// Multiple properties at once
78- err := errors.New ( " database error " ).
74+ err4 := errors.From (ErrDatabaseError ).
7975 WithProperties (map [string ]any{
80- " host" : " localhost" ,
81- " port" : 5432 ,
76+ " host" : " localhost" ,
77+ " port" : 5432 ,
8278 " database" : " myapp" ,
8379 }).
8480 Throw ()
@@ -87,11 +83,13 @@ err := errors.New("database error").
8783### Error Wrapping
8884
8985``` go
90- func getUserByID (id int ) (*User , error ) {
86+ var ErrUserNotFound = errors.New (" user not found" )
87+
88+ func getUserByID (id string ) (*User , error ) {
9189 user , err := db.Query (id)
9290 if err != nil {
93- return nil , errors.New ( " user not found " ).
94- WithIdentifier (404 ).
91+ return nil , errors.From (ErrUserNotFound ).
92+ WithIdentifier (404000 ).
9593 CausedBy (err).
9694 Throw ()
9795 }
@@ -120,16 +118,18 @@ func getUser(id int) (*User, error) {
120118### Stack Traces
121119
122120``` go
121+ var ErrSomethingWentWrong = errors.New (" something went wrong" )
122+
123123func layer3 () error {
124- return errors.New ( " something went wrong " ).Throw ()
124+ return errors.From (ErrSomethingWentWrong ).Throw ()
125125}
126126
127127func layer2 () error {
128- err := layer3 ()
129- if err != nil {
130- return errors.Stamp (err) // Adds layer2's location to stack
131- }
132- return nil
128+ err := layer3 ()
129+ if err != nil {
130+ return errors.Stamp (err) // Adds layer2's location to stack
131+ }
132+ return nil
133133}
134134
135135func layer1 () error {
@@ -147,7 +147,7 @@ func layer1() error {
147147
148148``` go
149149// Using errors.Is for comparison
150- notFoundErr := errors.New (" not found" ). WithIdentifier ( 404 )
150+ notFoundErr := errors.New (" not found" )
151151
152152if errors.Is (err, notFoundErr) {
153153 // Handle not found error
@@ -165,9 +165,6 @@ if errors.As(err, &e) {
165165 fmt.Printf (" Detail %d : %s \n " , i, detail)
166166 }
167167}
168-
169- // Unwrapping errors
170- cause := errors.Unwrap (err)
171168```
172169
173170### Converting Standard Errors
@@ -179,7 +176,7 @@ err := errors.From(stdErr).
179176 WithDetail (" additional context" ).
180177 Throw ()
181178
182- // Using Intercept() when you're unsure of the error type
179+ // Using Intercept() to complete the error
183180func handleError (err error ) error {
184181 e := errors.Intercept (err)
185182 e.WithProperty (" handled_at" , time.Now ())
0 commit comments