-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_example_test.go
More file actions
84 lines (65 loc) · 2.42 KB
/
errors_example_test.go
File metadata and controls
84 lines (65 loc) · 2.42 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package errors_test
import (
"fmt"
"github.com/axkit/errors"
)
func ExampleError_Error() {
type Input struct {
ID int `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
var ErrEmptyAttribute = errors.Template("empty attribute value").Code("CMN-0400")
var ErrInvalidInput = errors.Template("invalid input").Code("CMN-0400")
validateInput := func(inp *Input) error {
if inp.ID == 0 {
return ErrEmptyAttribute.New().Set("emptyFields", []string{"id"})
}
return nil
}
if err := validateInput(&Input{}); err != nil {
returnErr := ErrInvalidInput.Wrap(err)
fmt.Println(returnErr.Error())
// Output: invalid input: empty attribute value
}
}
// ExampleToJSON demonstrates generating JSON output for an error.
func ExampleToJSON() {
jsonErr := errors.Template("User not found").Code("E404").StatusCode(404).Severity(errors.Tiny)
jsonOutput := errors.ToJSON(jsonErr, errors.WithAttributes(errors.AddFields))
fmt.Println("JSON Error:", string(jsonOutput))
// Output: JSON Error: {"msg":"User not found","severity":"tiny","code":"E404","statusCode":404}
}
// ExampleWrap demonstrates wrapping an error.
func ExampleWrap() {
innerErr := errors.Template("Database connection failed")
outerErr := errors.Template("Service initialization failed").Wrap(innerErr)
fmt.Println("Wrapped Error:", outerErr.Error())
// Output: Wrapped Error: Service initialization failed: Database connection failed
}
// ExamplePredefinedErrors demonstrates using predefined errors.
func ExampleErrorTemplate() {
var ErrDatabaseDown = errors.Template("Database is unreachable").
Code("DB-500").
StatusCode(500).
Severity(errors.Critical)
if err := openDatabase("pg:5432"); err != nil {
fmt.Println("Error:", ErrDatabaseDown.Wrap(err).Error())
// Output: Error: Database is unreachable: unable to connect to database
}
}
// ExampleAlarm demonstrates raising an alarm for critical errors.
type CustomAlarmer struct{}
func (c *CustomAlarmer) Alarm(err error) {
fmt.Println("Critical error:", err)
}
func ExampleAlarmer() {
errors.SetAlarmer(&CustomAlarmer{})
var ErrSystemFailure = errors.Template("system failure").Severity(errors.Critical)
ErrSystemFailure.New().Set("path", "/var/lib").Alarm()
// Output: Critical error: system failure
}
func openDatabase(connStr string) error {
var dbErr error
return errors.Wrap(dbErr, "unable to connect to database").Set("connectionString", connStr)
}