-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
60 lines (51 loc) · 1.48 KB
/
errors_test.go
File metadata and controls
60 lines (51 loc) · 1.48 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
package main
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/CHESSComputing/DataBookkeeping/dbs"
)
func helper() error {
err := errors.New("test")
return dbs.Error(err, dbs.GenericErrorCode, "message", "helper")
}
var ErrorTest = errors.New("test")
func helper2() error {
return fmt.Errorf("helper2 %w", ErrorTest)
}
// TestDBSError is based on the following examples
// https://gosamples.dev/check-error-type/
func TestDBSError(t *testing.T) {
dbsErr := helper()
var e *dbs.DBSError
if errors.As(dbsErr, &e) {
fmt.Println("dbsErr is type of DBSError")
} else {
t.Error("dbsErr did not trigger errors.As")
}
err := errors.New("test")
if errors.As(err, &e) {
t.Error("provided generic error is not type of DBSError")
}
err2 := helper2()
if errors.Is(err2, ErrorTest) {
fmt.Println("err is wrapper around ErrorTest")
} else {
t.Error("err is not wrapper around ErrorTest")
}
// print wrapper error
err3 := dbs.Error(dbsErr, dbs.ParseErrorCode, "wrapper", "TestDBSError")
if !strings.Contains(err3.Error(), "TestDBSError") {
t.Error("error does not contain reason")
}
if !strings.Contains(err3.Error(), "helper") {
t.Error("error does not contain reason for underlying helper function")
}
fmt.Println("Wrapped error:", err3.Error())
err4 := dbs.Error(nil, dbs.GenericErrorCode, "nil wrapper", "TestDBSError")
if !strings.Contains(err4.Error(), "nil wrapper") {
t.Error("error does not contain nil error")
}
fmt.Println("Wrapped error:", err4.Error())
}