forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Greg Weber
committed
Jan 7, 2025
1 parent
4304999
commit c701d45
Showing
10 changed files
with
163 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package slogerr | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func formatterPlusV(s fmt.State, verb rune, err error) { | ||
if f, ok := err.(fmt.Formatter); ok { | ||
f.Format(s, verb) | ||
} else { | ||
fmt.Fprintf(s, "%+v", err) | ||
} | ||
} | ||
|
||
type unwrapper interface { | ||
Unwrap() error | ||
} | ||
|
||
type unwraps interface { | ||
Unwrap() []error | ||
} | ||
|
||
type errorUnwrap interface { | ||
Unwrap() error | ||
// ErrorNoUnwrap is the error message component of the wrapping | ||
// It will be a prefix of Error() | ||
// If there is no message in the wrapping then this can return an empty string | ||
ErrorNoUnwrap() string | ||
} | ||
|
||
type stackTraceAware interface { | ||
HasStack() bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package slogerr_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/gregwebs/errors/slogerr" | ||
) | ||
|
||
func ExampleStructuredError() { | ||
err := slogerr.Wraps( | ||
errors.New("cause"), | ||
"structured", | ||
"key", "value", | ||
"int", 1, | ||
) | ||
|
||
fmt.Println(err.Error()) | ||
// Output: structured key=value int=1: cause | ||
} | ||
|
||
func ExampleNew() { | ||
err := slogerr.New( | ||
"cause", | ||
"key", "value", | ||
"int", 1, | ||
) | ||
|
||
fmt.Println(err.Error()) | ||
// Output: cause key=value int=1 | ||
} | ||
|
||
func ExampleSlogRecord() { | ||
err := slogerr.Wraps( | ||
errors.New("cause"), | ||
"structured", | ||
"key", "value", | ||
"int", 1, | ||
) | ||
|
||
rec := slogerr.SlogRecord(err) | ||
fmt.Println(rec.Message) | ||
// Output: | ||
// structured: cause | ||
} | ||
|
||
func ExampleWraps() { | ||
// Create a base error | ||
baseErr := fmt.Errorf("database connection failed") | ||
|
||
// Wrap the error with additional structured information | ||
wrappedErr := slogerr.Wraps(baseErr, "user authentication failed", | ||
"user_id", "123", | ||
"attempt", 3, | ||
) | ||
|
||
// Print the error | ||
fmt.Println(wrappedErr) | ||
// Output: user authentication failed user_id=123 attempt=3: database connection failed | ||
} |
Oops, something went wrong.