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
+10-18Lines changed: 10 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# go-xerrors
2
2
3
-
`go-xerrors` is an idiomatic and lightweight Go package designed to enhance error handling in Go applications. It provides functions and types that simplify common error handling tasks by adding support for stack traces, combining multiple errors (multi-errors), and offering flexible error wrapping capabilities. The package also includes utilities for streamlined panic handling. `go-xerrors` maintains full compatibility with Go's standard error handling features (Go 1.13+), including `errors.As`, `errors.Is`, and `errors.Unwrap`.
3
+
`go-xerrors` is an idiomatic and lightweight Go package designed to enhance error handling in Go applications. It provides functions and types that simplify common error handling tasks by adding support for stack traces, combining multiple errors, and simplifying working with panics. `go-xerrors` maintains full compatibility with Go's standard error handling features (Go 1.13+), including `errors.As`, `errors.Is`, and `errors.Unwrap`.
4
4
5
5
**Main Features:**
6
6
@@ -72,15 +72,14 @@ var ErrAccessDenied = xerrors.Message("access denied")
72
72
73
73
funcperformAction() error {
74
74
// ...
75
-
returnxerrors.New(ErrAccessDenied) // Wrap the sentinel to add a stack trace
75
+
return ErrAccessDenied
76
76
}
77
77
78
78
// ...
79
79
80
80
err:=performAction()
81
81
if errors.Is(err, ErrAccessDenied) {
82
82
log.Println("Operation failed due to access denial.")
83
-
xerrors.Print(err) // Prints the error with stack trace
84
83
}
85
84
```
86
85
@@ -90,19 +89,12 @@ The `xerrors.New` function can wrap existing errors, which is useful for adding
90
89
91
90
**Adding Stack Traces to Existing Errors:**
92
91
93
-
If you receive an error (like a sentinel error) that lacks a stack trace, you can wrap it using `xerrors.New`:
92
+
If you receive an error that lacks a stack trace, you can wrap it using `xerrors.New`:
94
93
95
94
```go
96
-
varErrResourceBusy = xerrors.Message("resource is busy")
97
-
98
-
// ...
99
-
originalErr:=checkResourceStatus() // Returns ErrResourceBusy without stack trace
100
-
if originalErr != nil {
101
-
// Wrap the original error to capture the stack trace at this point
102
-
errWithTrace:= xerrors.New(originalErr)
103
-
if errors.Is(errWithTrace, ErrResourceBusy) {
104
-
xerrors.Print(errWithTrace) // Prints "resource is busy" with stack trace
105
-
}
95
+
output, err:= json.Marshal(data)
96
+
if err != nil {
97
+
return xerrors.New("failed to marshal data", err)
106
98
}
107
99
```
108
100
@@ -112,7 +104,7 @@ Provide a descriptive string as the first argument to `xerrors.New`, followed by
112
104
113
105
```go
114
106
iferr:=updateUserProfile(user); err != nil {
115
-
return xerrors.New("failed to update user profile", err)// failed to update user profile: <original error>
107
+
return xerrors.New("failed to update user profile", err)
0 commit comments