Skip to content

Commit

Permalink
add AdobeStateLib:ERROR_INTERNAL returning response body contents
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed Mar 21, 2024
1 parent d2c55af commit 72e7b45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
13 changes: 6 additions & 7 deletions lib/AdobeState.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ async function _wrap (promise, params) {
logAndThrow(e)
}

handleResponse(response, params)
return response
return handleResponse(response, params)
}

/**
Expand All @@ -87,16 +86,16 @@ async function _wrap (promise, params) {
* @param {object} params the params to the network call
* @returns {void}
*/
function handleResponse (response, params) {
async function handleResponse (response, params) {
if (response.ok) {
return
return response
}

const copyParams = cloneDeep(params)
switch (response.status) {
case 404:
// do nothing, no exception on 404
break
// no exception on 404
return response
case 401:
return logAndThrow(new codes.ERROR_UNAUTHORIZED({ messageValues: ['underlying DB provider'], sdkDetails: copyParams }))
case 403:
Expand All @@ -106,7 +105,7 @@ function handleResponse (response, params) {
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 provider with status: ${response.status}`], sdkDetails: copyParams }))
return logAndThrow(new codes.ERROR_INTERNAL({ messageValues: [`unexpected response from provider with status: ${response.status} body: ${await response.text()}`], sdkDetails: copyParams }))
}
}

Expand Down
11 changes: 6 additions & 5 deletions test/AdobeState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ const wrapInFetchResponse = (body, options = {}) => {
}
}

const wrapInFetchError = (status) => {
const wrapInFetchError = (status, body) => {
return {
ok: false,
headers: {
get: () => 'fake req id'
},
json: async () => 'error',
text: async () => 'error',
json: async () => JSON.parse(body),
text: async () => body,
status
}
}
Expand Down Expand Up @@ -253,9 +253,10 @@ describe('put', () => {
test('coverage: unknown server error', async () => {
const key = 'some-key'
const value = 'some-value'
const responseBody = 'error: this is the response body'

mockExponentialBackoff.mockResolvedValue(wrapInFetchError(500))
await expect(store.put(key, value)).rejects.toThrow('[AdobeStateLib:ERROR_INTERNAL] unexpected response from provider with status: 500')
mockExponentialBackoff.mockResolvedValue(wrapInFetchError(500, responseBody))
await expect(store.put(key, value)).rejects.toThrow(`[AdobeStateLib:ERROR_INTERNAL] unexpected response from provider with status: 500 body: ${responseBody}`)
})

test('coverage: unknown error (fetch network failure)', async () => {
Expand Down

0 comments on commit 72e7b45

Please sign in to comment.