|
1 | 1 | import test from 'ava' |
2 | | -import { AxiosError, AxiosHeaders } from 'axios' |
| 2 | +import { AxiosError, AxiosHeaders, isAxiosError } from 'axios' |
3 | 3 | import { getTestServer } from 'fixtures/seam/connect/api.js' |
| 4 | +import nock from 'nock' |
4 | 5 |
|
5 | 6 | import { |
6 | 7 | errorInterceptor, |
@@ -126,4 +127,100 @@ test('SeamHttp: throws SeamHttpInvalidInputError on invalid input', async (t) => |
126 | 127 | t.deepEqual(err?.getValidationErrorMessages('device_ids'), [ |
127 | 128 | 'Expected array, received number', |
128 | 129 | ]) |
| 130 | + t.deepEqual(err?.validationErrors, [ |
| 131 | + { |
| 132 | + parameterName: 'device_ids', |
| 133 | + errorMessages: ['Expected array, received number'], |
| 134 | + }, |
| 135 | + ]) |
| 136 | +}) |
| 137 | + |
| 138 | +test('SeamHttp: errors retain the AxiosError as cause', async (t) => { |
| 139 | + const { seed, endpoint } = await getTestServer(t) |
| 140 | + |
| 141 | + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { |
| 142 | + endpoint, |
| 143 | + axiosRetryOptions: { |
| 144 | + retries: 0, |
| 145 | + }, |
| 146 | + }) |
| 147 | + |
| 148 | + const err = await t.throwsAsync( |
| 149 | + async () => await seam.devices.get({ device_id: 'unknown-device' }), |
| 150 | + { |
| 151 | + instanceOf: SeamHttpApiError, |
| 152 | + }, |
| 153 | + ) |
| 154 | + |
| 155 | + if (!isAxiosError(err?.cause)) { |
| 156 | + t.fail('Expected cause to be the AxiosError') |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + t.is(err.cause.response?.status, 404) |
| 161 | + t.truthy(err.cause.config) |
| 162 | +}) |
| 163 | + |
| 164 | +test('SeamHttp: unauthorized error surfaces the API error message', async (t) => { |
| 165 | + const { seed, endpoint } = await getTestServer(t) |
| 166 | + |
| 167 | + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { |
| 168 | + endpoint, |
| 169 | + axiosRetryOptions: { |
| 170 | + retries: 0, |
| 171 | + }, |
| 172 | + }) |
| 173 | + |
| 174 | + nock(endpoint) |
| 175 | + .get('/devices/get') |
| 176 | + .query(true) |
| 177 | + .reply( |
| 178 | + 401, |
| 179 | + { |
| 180 | + error: { |
| 181 | + type: 'unauthorized', |
| 182 | + message: 'Custom unauthorized message from the server', |
| 183 | + }, |
| 184 | + }, |
| 185 | + { 'Content-Type': 'application/json', 'seam-request-id': 'request-1' }, |
| 186 | + ) |
| 187 | + |
| 188 | + const err = await t.throwsAsync( |
| 189 | + async () => await seam.devices.get({ device_id: 'unknown-device' }), |
| 190 | + { |
| 191 | + instanceOf: SeamHttpUnauthorizedError, |
| 192 | + message: 'Custom unauthorized message from the server', |
| 193 | + }, |
| 194 | + ) |
| 195 | + |
| 196 | + t.is(err?.code, 'unauthorized') |
| 197 | + t.is(err?.statusCode, 401) |
| 198 | + t.is(err?.requestId, 'request-1') |
| 199 | + t.true(isAxiosError(err?.cause)) |
| 200 | +}) |
| 201 | + |
| 202 | +test('SeamHttp: unauthorized error falls back without an API error body', async (t) => { |
| 203 | + const { seed, endpoint } = await getTestServer(t) |
| 204 | + |
| 205 | + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { |
| 206 | + endpoint, |
| 207 | + axiosRetryOptions: { |
| 208 | + retries: 0, |
| 209 | + }, |
| 210 | + }) |
| 211 | + |
| 212 | + nock(endpoint) |
| 213 | + .get('/devices/get') |
| 214 | + .query(true) |
| 215 | + .reply(401, 'Unauthorized', { 'Content-Type': 'text/plain' }) |
| 216 | + |
| 217 | + const err = await t.throwsAsync( |
| 218 | + async () => await seam.devices.get({ device_id: 'unknown-device' }), |
| 219 | + { |
| 220 | + instanceOf: SeamHttpUnauthorizedError, |
| 221 | + message: 'Unauthorized', |
| 222 | + }, |
| 223 | + ) |
| 224 | + |
| 225 | + t.is(err?.code, 'unauthorized') |
129 | 226 | }) |
0 commit comments