|
| 1 | +package apierrors |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestHTTPErrors(t *testing.T) { |
| 12 | + sentinel := errors.New("sentinel") |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + from error |
| 16 | + exp *HTTPError |
| 17 | + }{ |
| 18 | + |
| 19 | + // Status, ErrorCode, fmtStr, args |
| 20 | + { |
| 21 | + from: NewHTTPError( |
| 22 | + http.StatusBadRequest, |
| 23 | + ErrorCodeBadJSON, |
| 24 | + "Unable to parse JSON: %v", |
| 25 | + errors.New("bad syntax"), |
| 26 | + ), |
| 27 | + exp: &HTTPError{ |
| 28 | + HTTPStatus: http.StatusBadRequest, |
| 29 | + ErrorCode: ErrorCodeBadJSON, |
| 30 | + Message: "Unable to parse JSON: bad syntax", |
| 31 | + }, |
| 32 | + }, |
| 33 | + |
| 34 | + // ErrorCode, fmtStr, args |
| 35 | + { |
| 36 | + from: NewBadRequestError( |
| 37 | + ErrorCodeBadJSON, |
| 38 | + "Unable to parse JSON: %v", |
| 39 | + errors.New("bad syntax"), |
| 40 | + ), |
| 41 | + exp: &HTTPError{ |
| 42 | + HTTPStatus: http.StatusBadRequest, |
| 43 | + ErrorCode: ErrorCodeBadJSON, |
| 44 | + Message: "Unable to parse JSON: bad syntax", |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + from: NewNotFoundError( |
| 49 | + ErrorCodeUnknown, |
| 50 | + "error: %v", |
| 51 | + sentinel, |
| 52 | + ), |
| 53 | + exp: &HTTPError{ |
| 54 | + HTTPStatus: http.StatusNotFound, |
| 55 | + ErrorCode: ErrorCodeUnknown, |
| 56 | + Message: "error: " + sentinel.Error(), |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + from: NewForbiddenError( |
| 61 | + ErrorCodeUnknown, |
| 62 | + "error: %v", |
| 63 | + sentinel, |
| 64 | + ), |
| 65 | + exp: &HTTPError{ |
| 66 | + HTTPStatus: http.StatusForbidden, |
| 67 | + ErrorCode: ErrorCodeUnknown, |
| 68 | + Message: "error: " + sentinel.Error(), |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + from: NewUnprocessableEntityError( |
| 73 | + ErrorCodeUnknown, |
| 74 | + "error: %v", |
| 75 | + sentinel, |
| 76 | + ), |
| 77 | + exp: &HTTPError{ |
| 78 | + HTTPStatus: http.StatusUnprocessableEntity, |
| 79 | + ErrorCode: ErrorCodeUnknown, |
| 80 | + Message: "error: " + sentinel.Error(), |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + from: NewTooManyRequestsError( |
| 85 | + ErrorCodeUnknown, |
| 86 | + "error: %v", |
| 87 | + sentinel, |
| 88 | + ), |
| 89 | + exp: &HTTPError{ |
| 90 | + HTTPStatus: http.StatusTooManyRequests, |
| 91 | + ErrorCode: ErrorCodeUnknown, |
| 92 | + Message: "error: " + sentinel.Error(), |
| 93 | + }, |
| 94 | + }, |
| 95 | + |
| 96 | + // fmtStr, args |
| 97 | + { |
| 98 | + from: NewInternalServerError( |
| 99 | + "error: %v", |
| 100 | + sentinel, |
| 101 | + ), |
| 102 | + exp: &HTTPError{ |
| 103 | + HTTPStatus: http.StatusInternalServerError, |
| 104 | + ErrorCode: ErrorCodeUnexpectedFailure, |
| 105 | + Message: "error: " + sentinel.Error(), |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + from: NewConflictError( |
| 110 | + "error: %v", |
| 111 | + sentinel, |
| 112 | + ), |
| 113 | + exp: &HTTPError{ |
| 114 | + HTTPStatus: http.StatusConflict, |
| 115 | + ErrorCode: ErrorCodeConflict, |
| 116 | + Message: "error: " + sentinel.Error(), |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for idx, test := range tests { |
| 122 | + t.Logf("tests #%v - from %v exp %#v", idx, test.from, test.exp) |
| 123 | + |
| 124 | + require.Error(t, test.exp) |
| 125 | + require.Error(t, test.from) |
| 126 | + |
| 127 | + exp := test.exp |
| 128 | + got, ok := test.from.(*HTTPError) |
| 129 | + if !ok { |
| 130 | + t.Fatalf("exp type %T, got %v", got, test.from) |
| 131 | + } |
| 132 | + |
| 133 | + require.Equal(t, exp.HTTPStatus, got.HTTPStatus) |
| 134 | + require.Equal(t, exp.ErrorCode, got.ErrorCode) |
| 135 | + require.Equal(t, exp.Message, got.Message) |
| 136 | + require.Equal(t, exp.Error(), got.Error()) |
| 137 | + require.Equal(t, exp.Cause(), got.Cause()) |
| 138 | + } |
| 139 | + |
| 140 | + // test Error() with internal message |
| 141 | + { |
| 142 | + err := NewHTTPError( |
| 143 | + http.StatusBadRequest, |
| 144 | + ErrorCodeBadJSON, |
| 145 | + "Unable to parse JSON: %v", |
| 146 | + errors.New("bad syntax"), |
| 147 | + ).WithInternalError(sentinel).WithInternalMessage(sentinel.Error()) |
| 148 | + |
| 149 | + require.Equal(t, err.Error(), sentinel.Error()) |
| 150 | + require.Equal(t, err.Cause(), sentinel) |
| 151 | + require.Equal(t, err.Is(sentinel), true) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestOAuthErrors(t *testing.T) { |
| 156 | + sentinel := errors.New("sentinel") |
| 157 | + |
| 158 | + { |
| 159 | + err := NewOAuthError( |
| 160 | + "oauth error", |
| 161 | + "oauth desc", |
| 162 | + ) |
| 163 | + |
| 164 | + require.Error(t, err) |
| 165 | + require.Equal(t, err.Error(), "oauth error: oauth desc") |
| 166 | + require.Equal(t, err.Cause(), err) |
| 167 | + } |
| 168 | + |
| 169 | + // test Error() with internal message |
| 170 | + { |
| 171 | + err := NewOAuthError( |
| 172 | + "oauth error", |
| 173 | + "oauth desc", |
| 174 | + ).WithInternalError(sentinel).WithInternalMessage(sentinel.Error()) |
| 175 | + |
| 176 | + require.Error(t, err) |
| 177 | + require.Equal(t, err.Error(), sentinel.Error()) |
| 178 | + require.Equal(t, err.Cause(), sentinel) |
| 179 | + } |
| 180 | +} |
0 commit comments