Skip to content

Commit 5a2abd2

Browse files
authored
Revert "fix!: error codes (#224)"
This reverts commit d271e3c.
1 parent d271e3c commit 5a2abd2

File tree

3 files changed

+27
-45
lines changed

3 files changed

+27
-45
lines changed

lib/AdobeState.js

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

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-
}
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 }))
131128
}
132-
// 500 errors
133-
return logAndThrow(new codes.ERROR_INTERNAL({ messageValues: [`unexpected response from State service with body: ${body}`], sdkDetails: copyParams }))
134129
}
135130

136131
/** @private */

lib/StateError.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ 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_UNAUTHORIZED', '%s')
60-
E('ERROR_FORBIDDEN', '%s')
61-
E('ERROR_PAYLOAD_TOO_LARGE', '%s')
62-
E('ERROR_REQUEST_RATE_TOO_HIGH', '%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.')
6364

6465
// eslint-disable-next-line jsdoc/require-jsdoc
6566
function logAndThrow (e) {

test/AdobeState.test.js

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

274-
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(401, 'myerror'))
274+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(401))
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] myerror'
280+
message: '[AdobeStateLib:ERROR_UNAUTHORIZED] you are not authorized to access State service'
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, 'myerror'))
288+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(403))
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_FORBIDDEN] myerror'
294+
message: '[AdobeStateLib:ERROR_BAD_CREDENTIALS] cannot access State service, make sure your credentials are valid'
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, 'myerror'))
302+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(413))
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] myerror'
308+
message: '[AdobeStateLib:ERROR_PAYLOAD_TOO_LARGE] key, value or request payload is too large State service'
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, 'myerror'))
316+
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(429))
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] 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}`
322+
message: '[AdobeStateLib:ERROR_REQUEST_RATE_TOO_HIGH] Request rate too high. Please retry after sometime.'
337323
}))
338324
})
339325

@@ -348,7 +334,7 @@ describe('put', () => {
348334
sdkDetails: expect.objectContaining({
349335
requestId: 'fake-req-id'
350336
}),
351-
message: `[AdobeStateLib:ERROR_INTERNAL] unexpected response from State service with body: ${responseBody}`
337+
message: `[AdobeStateLib:ERROR_INTERNAL] unexpected response from State service with status: 500 body: ${responseBody}`
352338
}))
353339
})
354340

0 commit comments

Comments
 (0)