-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples_test.go
80 lines (63 loc) · 2.64 KB
/
examples_test.go
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
package ginerr
import (
"context"
"errors"
"fmt"
"net/http"
)
var ErrDatabaseOverloaded = errors.New("database overloaded")
type InputValidationError struct {
// ...
}
func (m *InputValidationError) Error() string {
return "..."
}
func ExampleRegisterErrorHandler() {
// Write your error handlers
validationHandler := func(_ context.Context, err *InputValidationError) (int, any) {
return http.StatusBadRequest, "Your input was invalid: " + err.Error()
}
databaseOverloadedHandler := func(context.Context, error) (int, any) {
return http.StatusBadGateway, "Please try again later"
}
// Register the error handlers and supply an empty version of the type for type reference
RegisterErrorHandler(&InputValidationError{}, validationHandler)
RegisterErrorHandler(ErrDatabaseOverloaded, databaseOverloadedHandler)
// Return errors somewhere deep in your code
errA := fmt.Errorf("validation error: %w", &InputValidationError{})
errB := fmt.Errorf("could not connect to database: %w", ErrDatabaseOverloaded)
// In your HTTP handlers, instantiate responses and return those to the users
codeA, responseA := NewErrorResponse(context.Background(), errA)
codeB, responseB := NewErrorResponse(context.Background(), errB)
// Check the output
fmt.Printf("%d: %s\n", codeA, responseA)
fmt.Printf("%d: %s\n", codeB, responseB)
// Output:
// 400: Your input was invalid: ...
// 502: Please try again later
}
func ExampleRegisterErrorHandlerOn() {
registry := NewErrorRegistry()
// Write your error handlers
validationHandler := func(_ context.Context, err *InputValidationError) (int, any) {
return http.StatusBadRequest, "Your input was invalid: " + err.Error()
}
databaseOverloadedHandler := func(context.Context, error) (int, any) {
return http.StatusBadGateway, "please try again later"
}
// Register the error handlers and supply an empty version of the type for type reference
RegisterErrorHandlerOn(registry, &InputValidationError{}, validationHandler)
RegisterErrorHandlerOn(registry, ErrDatabaseOverloaded, databaseOverloadedHandler)
// Return errors somewhere deep in your code
errA := fmt.Errorf("validation error: %w", &InputValidationError{})
errB := fmt.Errorf("could not connect to database: %w", ErrDatabaseOverloaded)
// In your HTTP handlers, instantiate responses and return those to the users
codeA, responseA := NewErrorResponseFrom(context.Background(), registry, errA)
codeB, responseB := NewErrorResponseFrom(context.Background(), registry, errB)
// Check the output
fmt.Printf("%d: %s\n", codeA, responseA)
fmt.Printf("%d: %s\n", codeB, responseB)
// Output:
// 400: Your input was invalid: ...
// 502: please try again later
}