Skip to content

Commit d271e3c

Browse files
moritzrahoshazron
andauthored
fix!: error codes (#224)
Co-authored-by: Shazron Abdullah <[email protected]>
1 parent fbcf3c2 commit d271e3c

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

lib/AdobeState.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,25 @@ async function handleResponse (response, params) {
112112
const copyParams = cloneDeep(params)
113113
copyParams.requestId = response.headers.get(REQUEST_ID_HEADER)
114114

115-
switch (response.status) {
116-
case 404:
117-
return response
118-
case 401:
119-
return logAndThrow(new codes.ERROR_UNAUTHORIZED({ messageValues: ['State service'], sdkDetails: copyParams }))
120-
case 403:
121-
return logAndThrow(new codes.ERROR_BAD_CREDENTIALS({ messageValues: ['State service'], sdkDetails: copyParams }))
122-
case 413:
123-
return logAndThrow(new codes.ERROR_PAYLOAD_TOO_LARGE({ messageValues: ['State service'], sdkDetails: copyParams }))
124-
case 429:
125-
return logAndThrow(new codes.ERROR_REQUEST_RATE_TOO_HIGH({ sdkDetails: copyParams }))
126-
default: // 500 errors
127-
return logAndThrow(new codes.ERROR_INTERNAL({ messageValues: [`unexpected response from State service with status: ${response.status} body: ${await response.text()}`], sdkDetails: copyParams }))
115+
const body = await response.text()
116+
if (response.status < 500) {
117+
switch (response.status) {
118+
case 404:
119+
return response // 404s are not an error
120+
case 401:
121+
return logAndThrow(new codes.ERROR_UNAUTHORIZED({ messageValues: [body], sdkDetails: copyParams }))
122+
case 403:
123+
return logAndThrow(new codes.ERROR_FORBIDDEN({ messageValues: [body], sdkDetails: copyParams }))
124+
case 413:
125+
return logAndThrow(new codes.ERROR_PAYLOAD_TOO_LARGE({ messageValues: [body], sdkDetails: copyParams }))
126+
case 429:
127+
return logAndThrow(new codes.ERROR_REQUEST_RATE_TOO_HIGH({ messageValues: [body], sdkDetails: copyParams }))
128+
default: // other 4xx errors
129+
return logAndThrow(new codes.ERROR_BAD_REQUEST({ messageValues: [body], sdkDetails: copyParams }))
130+
}
128131
}
132+
// 500 errors
133+
return logAndThrow(new codes.ERROR_INTERNAL({ messageValues: [`unexpected response from State service with body: ${body}`], sdkDetails: copyParams }))
129134
}
130135

131136
/** @private */

lib/StateError.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ E('ERROR_INTERNAL', '%s')
5656
E('ERROR_BAD_REQUEST', '%s')
5757
E('ERROR_BAD_ARGUMENT', '%s')
5858
E('ERROR_UNEXPECTED_NOT_FOUND', '%s')
59-
E('ERROR_UNKNOWN_PROVIDER', '%s')
60-
E('ERROR_UNAUTHORIZED', 'you are not authorized to access %s')
61-
E('ERROR_BAD_CREDENTIALS', 'cannot access %s, make sure your credentials are valid')
62-
E('ERROR_PAYLOAD_TOO_LARGE', 'key, value or request payload is too large')
63-
E('ERROR_REQUEST_RATE_TOO_HIGH', 'Request rate too high. Please retry after sometime.')
59+
E('ERROR_UNAUTHORIZED', '%s')
60+
E('ERROR_FORBIDDEN', '%s')
61+
E('ERROR_PAYLOAD_TOO_LARGE', '%s')
62+
E('ERROR_REQUEST_RATE_TOO_HIGH', '%s')
6463

6564
// eslint-disable-next-line jsdoc/require-jsdoc
6665
function logAndThrow (e) {

test/AdobeState.test.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,55 +271,69 @@ describe('put', () => {
271271
const key = 'some-key'
272272
const value = 'some-value'
273273

274-
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(401))
274+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(401, 'myerror'))
275275
await expect(store.put(key, value)).rejects.toThrow(
276276
expect.objectContaining({
277277
sdkDetails: expect.objectContaining({
278278
requestId: 'fake-req-id'
279279
}),
280-
message: '[AdobeStateLib:ERROR_UNAUTHORIZED] you are not authorized to access State service'
280+
message: '[AdobeStateLib:ERROR_UNAUTHORIZED] myerror'
281281
}))
282282
})
283283

284284
test('coverage: 403 error', async () => {
285285
const key = 'some-key'
286286
const value = 'some-value'
287287

288-
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(403))
288+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(403, 'myerror'))
289289
await expect(store.put(key, value)).rejects.toThrow(
290290
expect.objectContaining({
291291
sdkDetails: expect.objectContaining({
292292
requestId: 'fake-req-id'
293293
}),
294-
message: '[AdobeStateLib:ERROR_BAD_CREDENTIALS] cannot access State service, make sure your credentials are valid'
294+
message: '[AdobeStateLib:ERROR_FORBIDDEN] myerror'
295295
}))
296296
})
297297

298298
test('coverage: 413 error', async () => {
299299
const key = 'some-key'
300300
const value = 'some-value'
301301

302-
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(413))
302+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(413, 'myerror'))
303303
await expect(store.put(key, value)).rejects.toThrow(
304304
expect.objectContaining({
305305
sdkDetails: expect.objectContaining({
306306
requestId: 'fake-req-id'
307307
}),
308-
message: '[AdobeStateLib:ERROR_PAYLOAD_TOO_LARGE] key, value or request payload is too large State service'
308+
message: '[AdobeStateLib:ERROR_PAYLOAD_TOO_LARGE] myerror'
309309
}))
310310
})
311311

312312
test('coverage: 429 error', async () => {
313313
const key = 'some-key'
314314
const value = 'some-value'
315315

316-
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(429))
316+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(429, 'myerror'))
317317
await expect(store.put(key, value)).rejects.toThrow(
318318
expect.objectContaining({
319319
sdkDetails: expect.objectContaining({
320320
requestId: 'fake-req-id'
321321
}),
322-
message: '[AdobeStateLib:ERROR_REQUEST_RATE_TOO_HIGH] Request rate too high. Please retry after sometime.'
322+
message: '[AdobeStateLib:ERROR_REQUEST_RATE_TOO_HIGH] myerror'
323+
}))
324+
})
325+
test('coverage: unknown user error', async () => {
326+
const key = 'some-key'
327+
const value = 'some-value'
328+
const responseBody = 'error: this is the response body'
329+
330+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(400, responseBody))
331+
await expect(store.put(key, value)).rejects.toThrow(
332+
expect.objectContaining({
333+
sdkDetails: expect.objectContaining({
334+
requestId: 'fake-req-id'
335+
}),
336+
message: `[AdobeStateLib:ERROR_BAD_REQUEST] ${responseBody}`
323337
}))
324338
})
325339

@@ -334,7 +348,7 @@ describe('put', () => {
334348
sdkDetails: expect.objectContaining({
335349
requestId: 'fake-req-id'
336350
}),
337-
message: `[AdobeStateLib:ERROR_INTERNAL] unexpected response from State service with status: 500 body: ${responseBody}`
351+
message: `[AdobeStateLib:ERROR_INTERNAL] unexpected response from State service with body: ${responseBody}`
338352
}))
339353
})
340354

0 commit comments

Comments
 (0)