Skip to content

Commit

Permalink
update responseFormatter to handle JSON.parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed May 17, 2023
1 parent 9341fd6 commit 7922357
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
40 changes: 23 additions & 17 deletions src/interceptors/response/responseFormatter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
mockPutioAPIClientError,
mockPutioAPIClientResponse,
createMockXMLHttpRequest,
} from '../../test-utils/mocks'
import { IPutioAPIClientError } from '../../client/types'
import { DEFAULT_CLIENT_OPTIONS } from '../../constants'
Expand Down Expand Up @@ -104,23 +105,6 @@ describe('interceptors/response/responseFormatter', () => {
})

it('sets error.data property corrrectly when the request doesnt have response object, but has a request object that is a valid XMLHttpRequest', () => {
function createMockXMLHttpRequest(
readyState: number,
status: number,
responseText: string,
) {
const xhr = new XMLHttpRequest()
return new Proxy(xhr, {
get(target, prop) {
if (prop === 'readyState') return readyState
if (prop === 'status') return status
if (prop === 'responseText') return responseText
// @ts-ignore
return target[prop]
},
})
}

const error: IPutioAPIClientError = {
...mockPutioAPIClientError,
response: undefined,
Expand All @@ -143,6 +127,28 @@ describe('interceptors/response/responseFormatter', () => {
`),
)
})

it('sets error.data property corrrectly when the request doesnt have response object, but has a request object that is a valid XMLHttpRequest', () => {
const error: IPutioAPIClientError = {
...mockPutioAPIClientError,
response: undefined,
request: createMockXMLHttpRequest(
4,
500,
'no meaningful response body',
),
}

responseFormatter.onRejected(error).catch(e =>
expect(e).toMatchInlineSnapshot(`
Object {
"error_message": "MOCK_MESSAGE",
"error_type": "MOCK_ERROR",
"status_code": 0,
}
`),
)
})
})

describe('royally fucked up cases', () => {
Expand Down
15 changes: 10 additions & 5 deletions src/interceptors/response/responseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ export const createResponseFormatter: PutioAPIClientResponseInterceptorFactory =
error.request.readyState === 4
) {
const { status, responseText } = error.request
const data = JSON.parse(responseText)

errorData = {
...errorData,
...data,
status_code: status,
try {
const data = JSON.parse(responseText)

errorData = {
...errorData,
...data,
status_code: status,
}
} catch (parseError) {
return Promise.reject(error)
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test-utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ export const createMockErrorResponse = (
return this.data
},
})

export function createMockXMLHttpRequest(
readyState: number,
status: number,
responseText: string,
) {
const xhr = new XMLHttpRequest()
return new Proxy(xhr, {
get(target, prop) {
if (prop === 'readyState') return readyState
if (prop === 'status') return status
if (prop === 'responseText') return responseText
// @ts-ignore
return target[prop]
},
})
}

0 comments on commit 7922357

Please sign in to comment.