diff --git a/lib/AdobeState.js b/lib/AdobeState.js index a3727a2..3a3a1ef 100644 --- a/lib/AdobeState.js +++ b/lib/AdobeState.js @@ -76,8 +76,7 @@ async function _wrap (promise, params) { logAndThrow(e) } - handleResponse(response, params) - return response + return handleResponse(response, params) } /** @@ -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: @@ -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 })) } } diff --git a/test/AdobeState.test.js b/test/AdobeState.test.js index f15df73..b434628 100644 --- a/test/AdobeState.test.js +++ b/test/AdobeState.test.js @@ -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 } } @@ -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 () => {