-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
84 lines (76 loc) · 2.31 KB
/
errors_test.go
File metadata and controls
84 lines (76 loc) · 2.31 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 norm
import (
"context"
"errors"
"testing"
"github.com/jackc/pgx/v5/pgconn"
)
func TestWrapPgError_PassThroughAndCircuitOpen(t *testing.T) {
// pass-through non-pg error
e := errors.New("x")
if wrapPgError(e, "q", nil) != e {
t.Fatalf("expected passthrough")
}
// circuit open
ce := circuitOpenErr
out := wrapPgError(ce, "q", nil)
oe, ok := out.(*ORMError)
if !ok || oe.Code != ErrCodeConnection {
t.Fatalf("expected connection code, got %#v", out)
}
}
func TestMapPgErrorCode(t *testing.T) {
cases := map[string]ErrorCode{
"23505": ErrCodeDuplicate,
"23503": ErrCodeConstraint,
"23514": ErrCodeConstraint,
"23502": ErrCodeConstraint,
"40001": ErrCodeTransaction,
"xxxxx": ErrCodeValidation,
}
for k, want := range cases {
if got := mapPgErrorCode(k); got != want {
t.Fatalf("code %s -> %v (got %v)", k, want, got)
}
}
}
func TestWrapPgError_ContextCanceled(t *testing.T) {
out := wrapPgError(context.Canceled, "q", nil)
oe, ok := out.(*ORMError)
if !ok || oe.Code != ErrCodeTransaction {
t.Fatalf("expected transaction code for context canceled, got %#v", out)
}
// also test context.DeadlineExceeded
out2 := wrapPgError(context.DeadlineExceeded, "q2", nil)
oe2, ok2 := out2.(*ORMError)
if !ok2 || oe2.Code != ErrCodeTransaction {
t.Fatalf("expected transaction code for deadline exceeded, got %#v", out2)
}
}
func TestWrapPgError_PgErrMapping(t *testing.T) {
// construct a pg error to hit the errors.As branch
pgErr := &pgconn.PgError{Code: "23505", Message: "dup"}
out := wrapPgError(pgErr, "q", []any{"a"})
oe, ok := out.(*ORMError)
if !ok || oe.Code != ErrCodeDuplicate || oe.Message == "" || len(oe.Args) != 1 || oe.Query != "q" {
t.Fatalf("unexpected wrap: %#v", out)
}
}
func TestORMError_Error(t *testing.T) {
e := &ORMError{Code: ErrCodeValidation, Message: "m"}
if e.Error() != "m" {
t.Fatalf("error() not matching message")
}
}
func TestORMError_Unwrap(t *testing.T) {
inner := errors.New("inner cause")
e := &ORMError{Code: ErrCodeConnection, Message: "wrapped", Internal: inner}
if !errors.Is(e, inner) {
t.Fatalf("Unwrap should allow errors.Is to find the internal error")
}
// nil internal should not panic
e2 := &ORMError{Code: ErrCodeValidation, Message: "no internal"}
if e2.Unwrap() != nil {
t.Fatalf("Unwrap of nil internal should return nil")
}
}