Skip to content

Commit

Permalink
fix(north): switch HTTP request error from Promise reject to Error th…
Browse files Browse the repository at this point in the history
…rown
  • Loading branch information
burgerni10 authored and Nicolas Burger committed Oct 19, 2022
1 parent 637dcc5 commit baf9c1a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 51 deletions.
27 changes: 20 additions & 7 deletions src/services/request/AxiosRequest.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,30 @@ class AxiosRequest extends BaseRequest {
try {
await axiosInstance(axiosOptions)
} catch (error) {
const responseError = {
responseError: !!error.response,
statusCode: error.response ? error.response.status : undefined,
error,
}
clearTimeout(cancelTimeout)
return Promise.reject(responseError)
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const responseError = new Error(`Axios response error: ${error.response.status}`)
responseError.responseError = true
responseError.statusCode = error.response.status
throw responseError
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
const requestError = new Error(error.message)
requestError.responseError = false
throw requestError
} else {
// Something happened in setting up the request that triggered an Error
const otherError = new Error(error.message)
otherError.responseError = false
throw otherError
}
}

clearTimeout(cancelTimeout)
return true
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/services/request/AxiosRequest.class.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('AxiosRequest', () => {
const mockAxios = jest.fn().mockReturnValue(Promise.resolve())
axios.create.mockReturnValue(mockAxios)

const result = await axiosRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
await axiosRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)

const expectedAxiosCreateOptions = {
cancelToken: 'token',
Expand All @@ -63,7 +63,6 @@ describe('AxiosRequest', () => {
expect(axios.create).toBeCalledTimes(1)
expect(axios.create).toBeCalledWith(expectedAxiosCreateOptions)
expect(mockAxios).toBeCalledWith(expectedAxiosOptions)
expect(result).toBeTruthy()
})

it('should properly call axios with proxy for JSON data', async () => {
Expand All @@ -85,7 +84,7 @@ describe('AxiosRequest', () => {
const timeout = 1000 * 10000
const mockAxios = jest.fn().mockReturnValue(Promise.resolve())
axios.create.mockReturnValue(mockAxios)
const result = await axiosRequest.sendImplementation(requestUrl, method, headers, proxy, data, timeout)
await axiosRequest.sendImplementation(requestUrl, method, headers, proxy, data, timeout)

const expectedAxiosCreateOptions1 = {
cancelToken: 'token',
Expand Down Expand Up @@ -116,7 +115,6 @@ describe('AxiosRequest', () => {
])
expect(tunnel.httpsOverHttps).toBeCalledWith({ axiosProxy: expectedAxiosProxy })
expect(mockAxios).toBeCalledWith(expectedAxiosOptions)
expect(result).toBeTruthy()
})

it('should properly call axios without proxy for form-data', async () => {
Expand All @@ -130,7 +128,7 @@ describe('AxiosRequest', () => {
const mockAxios = jest.fn().mockReturnValue(Promise.resolve())
axios.create.mockReturnValue(mockAxios)

const result = await axiosRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
await axiosRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)

const expectedAxiosOptions = {
method,
Expand All @@ -141,7 +139,6 @@ describe('AxiosRequest', () => {

expect(utils.generateFormDataBodyFromFile).toBeCalledWith(data)
expect(mockAxios).toBeCalledWith(expectedAxiosOptions)
expect(result).toBeTruthy()
})

it('should properly handle axios error', async () => {
Expand All @@ -165,11 +162,10 @@ describe('AxiosRequest', () => {
result = response
}

const expectedResult = {
error: { response: { status: 400 } },
responseError: true,
statusCode: 400,
}
const expectedResult = new Error('Axios response error: 400')
expectedResult.responseError = true
expectedResult.statusCode = 400

expect(result).toEqual(expectedResult)
})
})
27 changes: 11 additions & 16 deletions src/services/request/FetchRequest.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,20 @@ class FetchRequest extends BaseRequest {
timeout,
}

let response
try {
const response = await fetch(requestUrl, fetchOptions)
if (!response.ok) {
const responseError = {
responseError: true,
statusCode: response.status,
error: new Error(response.statusText),
}
return Promise.reject(responseError)
}
response = await fetch(requestUrl, fetchOptions)
} catch (error) {
const connectError = {
responseError: false,
error,
}
return Promise.reject(connectError)
const requestError = error
requestError.responseError = false
throw requestError
}
if (!response.ok) {
const responseError = new Error(response.statusText)
responseError.responseError = true
responseError.statusCode = response.status
throw responseError
}

return true
}
}

Expand Down
27 changes: 10 additions & 17 deletions src/services/request/FetchRequest.class.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('FetchRequest', () => {
const timeout = 1000 * 10000
fetch.mockReturnValue(Promise.resolve(new Response('Ok')))

const result = await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)

const expectedFetchOptions = {
method,
Expand All @@ -56,7 +56,6 @@ describe('FetchRequest', () => {
}

expect(fetch).toBeCalledWith(requestUrl, expectedFetchOptions)
expect(result).toBeTruthy()
})

it('should properly call node-fetch with proxy for JSON data', async () => {
Expand All @@ -78,7 +77,7 @@ describe('FetchRequest', () => {
const timeout = 1000 * 10000
fetch.mockReturnValue(Promise.resolve(new Response('Ok')))

const result = await fetchRequest.sendImplementation(requestUrl, method, headers, proxy, data, timeout)
await fetchRequest.sendImplementation(requestUrl, method, headers, proxy, data, timeout)

const expectedProxyOptions = {
auth: 'username:password',
Expand All @@ -104,7 +103,6 @@ describe('FetchRequest', () => {

expect(ProxyAgent).toBeCalledWith(expectedProxyOptions)
expect(fetch).toBeCalledWith(requestUrl, expectedFetchOptions)
expect(result).toBeTruthy()
})

it('should properly call node-fetch without proxy for form-data', async () => {
Expand All @@ -117,7 +115,7 @@ describe('FetchRequest', () => {
const timeout = 1000 * 10000
fetch.mockReturnValue(Promise.resolve(new Response('Ok')))

const result = await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)

const expectedFetchOptions = {
method,
Expand All @@ -129,7 +127,6 @@ describe('FetchRequest', () => {

expect(utils.generateFormDataBodyFromFile).toBeCalledWith(data)
expect(fetch).toBeCalledWith(requestUrl, expectedFetchOptions)
expect(result).toBeTruthy()
})

it('should properly handle fetch response error', async () => {
Expand All @@ -145,16 +142,14 @@ describe('FetchRequest', () => {

let result
try {
result = await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
} catch (response) {
result = response
}

const expectedResult = {
error: new Error('statusText'),
responseError: true,
statusCode: 400,
}
const expectedResult = new Error('statusText')
expectedResult.responseError = true
expectedResult.statusCode = 400
expect(result).toEqual(expectedResult)
})

Expand All @@ -171,15 +166,13 @@ describe('FetchRequest', () => {

let result
try {
result = await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
await fetchRequest.sendImplementation(requestUrl, method, headers, null, data, timeout)
} catch (response) {
result = response
}

const expectedResult = {
error: new Error('error'),
responseError: false,
}
const expectedResult = new Error('error')
expectedResult.responseError = false
expect(result).toEqual(expectedResult)
})
})

0 comments on commit baf9c1a

Please sign in to comment.