-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
47 lines (47 loc) · 1.5 KB
/
doc.go
File metadata and controls
47 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package errors provides a lightweight error handling library with support for
// error wrapping, formatting, and error chain checking.
//
// # DO NOT use fmt.Errorf to create errors, use errors.New or errors.Errorf instead.
//
// fmt.Errorf is not compatible with errors.Is and errors.As.
//
// fmt.Errorf("failed to process user %d: %w", userID, err)
//
// is not the same as
//
// errors.Errorf("failed to process user %d: %w", userID, err)
//
// # Example:
//
// // Create an error
// err := errors.New("user validation failed").
// With("user_id", 12345).
// With("email", "user@example.com").
// With("attempt", 3)
//
// // Wrap the error
// err = errors.Wrap(err, "using wrap to wrap the error")
// err = errors.Errorf("%w can also wrap the error", err)
//
// // Format the error
// message := err.Error()
// textWithStack := errors.Format(err)
// jsonWithStack := errors.FormatJSON(err)
// colorizedWithStack := errors.FormatColorized(err)
//
// // using Sprintf to format the error
// message := fmt.Sprintf("%s", err)
// textWithStack := fmt.Sprintf("%v", err) // equal to errors.Format(err)
// jsonWithStack := fmt.Sprintf("%#v", err) // equal to errors.FormatJSON(err)
// colorizedWithStack := fmt.Sprintf("%+v", err) // equal to errors.FormatColorized(err)
//
// // Check if the error is a specific error
// if errors.Is(err, errors.New("user validation failed")) {
// // handle the error
// }
//
// var validationErr AnErrorType
// if errors.As(err, &validationErr) {
// // handle the error
// }
package errors