Skip to content

Commit

Permalink
feat: timeout-error
Browse files Browse the repository at this point in the history
closes linear #UI-739

-add condition to catch axios timeout error
-add test to cover condition
  • Loading branch information
ozhanefemeral committed Oct 4, 2023
1 parent a7bdc72 commit 190e912
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
38 changes: 35 additions & 3 deletions src/interceptors/response/responseFormatter.spec.ts
Expand Up @@ -2,6 +2,7 @@ import {
mockPutioAPIClientError,
mockPutioAPIClientResponse,
createMockXMLHttpRequest,
mockAxiosError,
} from '../../test-utils/mocks'
import { IPutioAPIClientError } from '../../client/types'
import { DEFAULT_CLIENT_OPTIONS } from '../../constants'
Expand Down Expand Up @@ -35,7 +36,38 @@ describe('interceptors/response/responseFormatter', () => {
})

describe('failed responses', () => {
it('sets error.data property correctly when the request failed with Put.io API signature', () => {
it('sets error.data property correctly when the request failed with client timeout error', () => {
const mockAxiosErrorWithTimeout = {
...mockAxiosError,
code: 'ECONNABORTED',
}

const mockPutioAPIClientErrorWithTimeout = {
...mockAxiosErrorWithTimeout,
...mockPutioAPIClientError,
}

const error = {
...mockPutioAPIClientErrorWithTimeout,
response: undefined,
request: undefined,
config: {},
}

responseFormatter.onRejected(error).catch(e => {
expect(e).toMatchInlineSnapshot(`
Object {
"error_message": "Request timed out",
"error_type": "ERROR",
"extra": Object {},
"status_code": 408,
"x-trace-id": undefined,
}
`)
})
})

it('sets error.data property correctly when the request failed with put.io API signature', () => {
const error = {
...mockPutioAPIClientError,
response: {
Expand Down Expand Up @@ -69,7 +101,7 @@ describe('interceptors/response/responseFormatter', () => {
)
})

it('sets error.data property correctly when the request failed with HTTP response but without Put.io API signature and no meaningful header config', () => {
it('sets error.data property correctly when the request failed with HTTP response but without put.io API signature and no meaningful header config', () => {
const error: IPutioAPIClientError = {
...mockPutioAPIClientError,
response: {
Expand All @@ -96,7 +128,7 @@ describe('interceptors/response/responseFormatter', () => {
)
})

it('sets error.data property correctly when the request failed without Put.io API signature', () => {
it('sets error.data property correctly when the request failed without put.io API signature', () => {
responseFormatter.onRejected(mockPutioAPIClientError).catch(e =>
expect(e).toMatchInlineSnapshot(`
Object {
Expand Down
11 changes: 10 additions & 1 deletion src/interceptors/response/responseFormatter.ts
Expand Up @@ -12,7 +12,7 @@ export const createResponseFormatter: PutioAPIClientResponseInterceptorFactory =
body: response.data,
}),

onRejected: (error: Error) => {
onRejected: (error: unknown) => {
if (!axios.isAxiosError(error)) {
return Promise.reject(error)
}
Expand All @@ -26,6 +26,15 @@ export const createResponseFormatter: PutioAPIClientResponseInterceptorFactory =
extra: {},
}

// ECONNABORTED is the code for a request that timed out in axios.
if (error.code === 'ECONNABORTED') {
errorData = {
...errorData,
status_code: 408,
error_message: 'Request timed out',
}
}

if (error.response && error.response.data) {
const { status, data } = error.response
errorData = isPutioAPIErrorResponse(data)
Expand Down

0 comments on commit 190e912

Please sign in to comment.