Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: error codes #224

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions lib/AdobeState.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,25 @@ async function handleResponse (response, params) {
const copyParams = cloneDeep(params)
copyParams.requestId = response.headers.get(REQUEST_ID_HEADER)

switch (response.status) {
case 404:
return response
case 401:
return logAndThrow(new codes.ERROR_UNAUTHORIZED({ messageValues: ['State service'], sdkDetails: copyParams }))
case 403:
return logAndThrow(new codes.ERROR_BAD_CREDENTIALS({ messageValues: ['State service'], sdkDetails: copyParams }))
case 413:
return logAndThrow(new codes.ERROR_PAYLOAD_TOO_LARGE({ messageValues: ['State service'], sdkDetails: copyParams }))
case 429:
return logAndThrow(new codes.ERROR_REQUEST_RATE_TOO_HIGH({ sdkDetails: copyParams }))
default: // 500 errors
return logAndThrow(new codes.ERROR_INTERNAL({ messageValues: [`unexpected response from State service with status: ${response.status} body: ${await response.text()}`], sdkDetails: copyParams }))
const body = await response.text()
if (response.status < 500) {
switch (response.status) {
case 404:
return response // 404s are not an error
case 401:
return logAndThrow(new codes.ERROR_UNAUTHORIZED({ messageValues: [body], sdkDetails: copyParams }))
case 403:
return logAndThrow(new codes.ERROR_FORBIDDEN({ messageValues: [body], sdkDetails: copyParams }))
case 413:
return logAndThrow(new codes.ERROR_PAYLOAD_TOO_LARGE({ messageValues: [body], sdkDetails: copyParams }))
case 429:
return logAndThrow(new codes.ERROR_REQUEST_RATE_TOO_HIGH({ messageValues: [body], sdkDetails: copyParams }))
default: // other 4xx errors
return logAndThrow(new codes.ERROR_BAD_REQUEST({ messageValues: [body], sdkDetails: copyParams }))
}
}
// 500 errors
return logAndThrow(new codes.ERROR_INTERNAL({ messageValues: [`unexpected response from State service with body: ${body}`], sdkDetails: copyParams }))
}

/** @private */
Expand Down
9 changes: 4 additions & 5 deletions lib/StateError.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ E('ERROR_INTERNAL', '%s')
E('ERROR_BAD_REQUEST', '%s')
E('ERROR_BAD_ARGUMENT', '%s')
E('ERROR_UNEXPECTED_NOT_FOUND', '%s')
E('ERROR_UNKNOWN_PROVIDER', '%s')
E('ERROR_UNAUTHORIZED', 'you are not authorized to access %s')
E('ERROR_BAD_CREDENTIALS', 'cannot access %s, make sure your credentials are valid')
E('ERROR_PAYLOAD_TOO_LARGE', 'key, value or request payload is too large')
E('ERROR_REQUEST_RATE_TOO_HIGH', 'Request rate too high. Please retry after sometime.')
E('ERROR_UNAUTHORIZED', '%s')
E('ERROR_FORBIDDEN', '%s')
E('ERROR_PAYLOAD_TOO_LARGE', '%s')
E('ERROR_REQUEST_RATE_TOO_HIGH', '%s')

// eslint-disable-next-line jsdoc/require-jsdoc
function logAndThrow (e) {
Expand Down
32 changes: 23 additions & 9 deletions test/AdobeState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,55 +271,69 @@ describe('put', () => {
const key = 'some-key'
const value = 'some-value'

mockExponentialBackoff.mockResolvedValue(wrapInFetchError(401))
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(401, 'myerror'))
await expect(store.put(key, value)).rejects.toThrow(
expect.objectContaining({
sdkDetails: expect.objectContaining({
requestId: 'fake-req-id'
}),
message: '[AdobeStateLib:ERROR_UNAUTHORIZED] you are not authorized to access State service'
message: '[AdobeStateLib:ERROR_UNAUTHORIZED] myerror'
}))
})

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

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

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

mockExponentialBackoff.mockResolvedValue(wrapInFetchError(413))
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(413, 'myerror'))
await expect(store.put(key, value)).rejects.toThrow(
expect.objectContaining({
sdkDetails: expect.objectContaining({
requestId: 'fake-req-id'
}),
message: '[AdobeStateLib:ERROR_PAYLOAD_TOO_LARGE] key, value or request payload is too large State service'
message: '[AdobeStateLib:ERROR_PAYLOAD_TOO_LARGE] myerror'
}))
})

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

mockExponentialBackoff.mockResolvedValue(wrapInFetchError(429))
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(429, 'myerror'))
await expect(store.put(key, value)).rejects.toThrow(
expect.objectContaining({
sdkDetails: expect.objectContaining({
requestId: 'fake-req-id'
}),
message: '[AdobeStateLib:ERROR_REQUEST_RATE_TOO_HIGH] Request rate too high. Please retry after sometime.'
message: '[AdobeStateLib:ERROR_REQUEST_RATE_TOO_HIGH] myerror'
}))
})
test('coverage: unknown user error', async () => {
const key = 'some-key'
const value = 'some-value'
const responseBody = 'error: this is the response body'

mockExponentialBackoff.mockResolvedValue(wrapInFetchError(400, responseBody))
await expect(store.put(key, value)).rejects.toThrow(
expect.objectContaining({
sdkDetails: expect.objectContaining({
requestId: 'fake-req-id'
}),
message: `[AdobeStateLib:ERROR_BAD_REQUEST] ${responseBody}`
}))
})

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

Expand Down