github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/net/http/httperror/httperror_test.go (about) 1 package httperror 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/bytom/bytom/errors" 9 ) 10 11 var ( 12 errNotFound = errors.New("not found") 13 testFormatter = Formatter{ 14 Default: Info{500, "CH000", "Internal server error"}, 15 IsTemporary: func(Info, error) bool { return false }, 16 Errors: map[error]Info{ 17 errNotFound: {400, "CH002", "Not found"}, 18 }, 19 } 20 ) 21 22 // Dummy error type, to test that Format 23 // doesn't panic when it's used as a map key. 24 type sliceError []int 25 26 func (err sliceError) Error() string { return "slice error" } 27 28 func TestInfo(t *testing.T) { 29 cases := []struct { 30 err error 31 want int 32 }{ 33 {nil, 500}, 34 {context.Canceled, 500}, 35 {errNotFound, 400}, 36 {errors.Wrap(errNotFound, "foo"), 400}, 37 {sliceError{}, 500}, 38 {fmt.Errorf("an error!"), 500}, 39 } 40 41 for _, test := range cases { 42 resp := testFormatter.Format(test.err) 43 got := resp.HTTPStatus 44 if got != test.want { 45 t.Errorf("errInfo(%#v) = %d want %d", test.err, got, test.want) 46 } 47 } 48 }