...
1 package apperror_test
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/pkg/errors"
8
9 "github.com/stretchr/testify/assert"
10
11 "edge-infra.dev/pkg/edge/iam/apperror"
12 )
13
14 func TestErrorChainWhenWrapped(t *testing.T) {
15 dbErr := errors.New("(user db) -> user not found in db")
16 svcErr := fmt.Errorf("(user service) -> %w", dbErr)
17 handlerErr := apperror.NewStatusError(svcErr, 404)
18
19 errChain := apperror.ErrorChain(handlerErr)
20
21 assert.Equal(t, "[404]: (user service) -> (user db) -> user not found in db", errChain)
22 assert.Equal(t, "(user service) -> (user db) -> user not found in db", handlerErr.Error())
23 }
24
25 func TestErrorChainWithAppError(t *testing.T) {
26
27 err := errors.New("SQL_ERR::NO_ROWS")
28 dbErr := apperror.New(err, "not_found", "user not found in db")
29
30 svcErr := fmt.Errorf("(user service) -> %w", dbErr)
31
32 handlerErr := apperror.NewStatusError(svcErr, 404)
33
34 errChain := apperror.ErrorChain(handlerErr)
35
36 assert.Equal(t, "[404]: (user service) -> user not found in db <not_found>. SQL_ERR::NO_ROWS", errChain)
37 assert.Equal(t, "(user service) -> user not found in db <not_found>. SQL_ERR::NO_ROWS", handlerErr.Error())
38 }
39
40 func TestAppError(t *testing.T) {
41 dbErr := errors.New("SQL_ERR::NO_ROWS")
42 appErr := apperror.New(dbErr, "not_found", "user not found in db")
43 assert.Equal(t, "user not found in db <not_found>. SQL_ERR::NO_ROWS", appErr.Error())
44 }
45
46 func TestErrorCode(t *testing.T) {
47 dbErr := errors.New("SQL_ERR::NO_ROWS")
48 appErr := apperror.New(dbErr, "not_found", "user not found in db")
49
50 assert.Equal(t, "", apperror.ErrorCode(dbErr))
51 assert.Equal(t, "not_found", apperror.ErrorCode(appErr))
52 assert.Equal(t, "user not found in db <not_found>. SQL_ERR::NO_ROWS", appErr.Error())
53 }
54
View as plain text