Add error formatting to serrors examples
@@ -215,20 +215,34 @@ func ExampleNew() {
// That is to prevent that errors with same message in different packages
// with same text are seen as the same thing:
fmt.Println(xerrors.Is(err1, err2))
-
// Output:
// true
// true
// false
}
+func ExampleWithCtx() {
+ // ErrBadL4 is an error defined at package scope.
+ var ErrBadL4 = serrors.New("Unsupported L4 protocol")
+ addedCtx := serrors.WithCtx(ErrBadL4, "type", "SCTP")
+
+ fmt.Println(addedCtx)
+ // Output:
+ // Unsupported L4 protocol type="SCTP"
+}
+
func ExampleWrapStr() {
// ErrNoSpace is an error defined at package scope.
var ErrNoSpace = serrors.New("no space")
-
wrappedErr := serrors.WrapStr("wrap with more context", ErrNoSpace, "ctx", 1)
+
fmt.Println(xerrors.Is(wrappedErr, ErrNoSpace))
- // Output: true
+ fmt.Printf("\n%v", wrappedErr)
+ // Output:
+ // true
+ //
+ // wrap with more context ctx="1"
+ // no space
}
func ExampleWrap() {
@@ -236,15 +250,18 @@ func ExampleWrap() {
var ErrNoSpace = serrors.New("no space")
// ErrDB is an error defined at package scope.
var ErrDB = serrors.New("db")
-
wrapped := serrors.Wrap(ErrDB, ErrNoSpace, "ctx", 1)
+
// Now we can identify specific errors:
fmt.Println(xerrors.Is(wrapped, ErrNoSpace))
-
// But we can also identify the broader error class ErrDB:
fmt.Println(xerrors.Is(wrapped, ErrDB))
+ fmt.Printf("\n%v", wrapped)
// Output:
// true
// true
+ //
+ // db ctx="1"
+ // no space
}
