Skip to content

Commit 2e5f512

Browse files
committed
Update documentation
1 parent 6497bdd commit 2e5f512

4 files changed

Lines changed: 18 additions & 26 deletions

File tree

README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go-xerrors
22

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`.
44

55
**Main Features:**
66

@@ -72,15 +72,14 @@ var ErrAccessDenied = xerrors.Message("access denied")
7272

7373
func performAction() error {
7474
// ...
75-
return xerrors.New(ErrAccessDenied) // Wrap the sentinel to add a stack trace
75+
return ErrAccessDenied
7676
}
7777

7878
// ...
7979

8080
err := performAction()
8181
if errors.Is(err, ErrAccessDenied) {
8282
log.Println("Operation failed due to access denial.")
83-
xerrors.Print(err) // Prints the error with stack trace
8483
}
8584
```
8685

@@ -90,19 +89,12 @@ The `xerrors.New` function can wrap existing errors, which is useful for adding
9089

9190
**Adding Stack Traces to Existing Errors:**
9291

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`:
9493

9594
```go
96-
var ErrResourceBusy = 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)
10698
}
10799
```
108100

@@ -112,7 +104,7 @@ Provide a descriptive string as the first argument to `xerrors.New`, followed by
112104

113105
```go
114106
if err := 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)
116108
}
117109
```
118110

@@ -124,10 +116,10 @@ if err := updateUserProfile(user); err != nil {
124116
var ErrConnectionFailed = xerrors.Message("connection failed")
125117
var ErrTimeout = xerrors.Message("operation timed out")
126118

127-
// Wrap both errors under a single contextual message
128-
combinedErr := xerrors.New("data retrieval failed", ErrConnectionFailed, ErrTimeout)
119+
// Wrap both errors under a single error
120+
combinedErr := xerrors.New(ErrConnectionFailed, ErrTimeout)
129121

130-
fmt.Println(combinedErr.Error()) // data retrieval failed: connection failed: operation timed out
122+
fmt.Println(combinedErr.Error()) // connection failed: operation timed out
131123
```
132124

133125
This feature is useful when a high-level operation fails due to multiple underlying issues that need to be reported together.

doc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Package xerrors is an idiomatic and lightweight package that
2-
// provides a set of functions to make working with errors easier.
3-
// It adds support for stack traces, [multierror]s, and simplifies
4-
// working with wrapped errors and panics. The `go-xerrors` package
5-
// is fully compatible with Go errors 1.13, supporting the
6-
// `errors.As`, `errors.Is`, and `errors.Unwrap` functions.
1+
// Package xerrors is an idiomatic and lightweight Go package designed to
2+
// enhance error handling in Go applications. It provides functions and types
3+
// that simplify common error handling tasks by adding support for stack
4+
// traces, combining multiple errors, and simplifying working with panics.
5+
// The package maintains full compatibility with Go's standard error handling
6+
// features (Go 1.13+), including errors.As, errors.Is, and errors.Unwrap.
77
package xerrors

stacktrace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (e *withStackTrace) ErrorDetails() string {
5757
return e.stack.String()
5858
}
5959

60-
// Unwrap implements the Go 1.13 `Unwrap() []error` method, returning
60+
// Unwrap implements the Go 1.13 `Unwrap() error` method, returning
6161
// the wrapped error.
6262
func (e *withStackTrace) Unwrap() error {
6363
return e.err

wrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (e *withWrapper) Error() string {
4242
return s.String()
4343
}
4444

45-
// Unwrap implements the Go 1.13 `Unwrap() []error` method, returning
45+
// Unwrap implements the Go 1.13 `Unwrap() error` method, returning
4646
// the wrapped error.
4747
func (e *withWrapper) Unwrap() error {
4848
return e.err

0 commit comments

Comments
 (0)