Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handling of backend errors #191

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const checkPackageUpdates = async (
const pkgName = packageName ?? name
const response = await request<{ version: string }>(
`https://registry.npmjs.org/${pkgName}/latest`,
{ skipTrackingHeaders: true }
{ skipTrackingHeaders: true, disableLiFiErrorCodes: true }
)
const latestVersion = response.version
const currentVersion = packageVersion ?? version
Expand Down Expand Up @@ -62,7 +62,8 @@ export const convertQuoteToRoute = (step: LiFiStep): Route => {

export const fetchTxErrorDetails = async (txHash: string, chainId: number) => {
const response = await request<TenderlyResponse>(
`https://api.tenderly.co/api/v1/public-contract/${chainId}/tx/${txHash}`
`https://api.tenderly.co/api/v1/public-contract/${chainId}/tx/${txHash}`,
{ disableLiFiErrorCodes: true }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the request function in a couple of places to make requests from sources outside of the LiFi domain - This flag opts out of using the LiFi error codes in situations like this.

)

return response
Expand Down
35 changes: 23 additions & 12 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { config } from './config.js'
import { HTTPError } from './utils/errors.js'
import { HTTPError } from './utils/httpError.js'
import { wait } from './utils/utils.js'
import { ValidationError } from './utils/errors.js'
import type { ExtendedRequestInit } from './types/request.js'
import { version } from './version.js'

export const requestSettings = {
retries: 1,
}

interface ExtendedRequestInit extends RequestInit {
retries?: number
skipTrackingHeaders?: boolean
}
const stripExtendRequestInitProperties = ({
retries,
skipTrackingHeaders,
disableLiFiErrorCodes,
...rest
}: ExtendedRequestInit): RequestInit => ({
...rest,
})

export const request = async <T = Response>(
url: RequestInfo | URL,
Expand All @@ -20,8 +26,8 @@ export const request = async <T = Response>(
): Promise<T> => {
const { userId, integrator, widgetVersion, apiKey } = config.get()
if (!integrator) {
throw new Error(
'Integrator not found. Please see documentation https://docs.li.fi/integrate-li.fi-js-sdk/set-up-the-sdk'
throw new ValidationError(
'You need to provide the Integrator property. Please see documentation https://docs.li.fi/integrate-li.fi-js-sdk/set-up-the-sdk'
)
}
options.retries = options.retries ?? requestSettings.retries
Expand Down Expand Up @@ -62,18 +68,23 @@ export const request = async <T = Response>(
}
}

const response: Response = await fetch(url, options)
const response: Response = await fetch(
url,
stripExtendRequestInitProperties(options)
)
if (!response.ok) {
throw new HTTPError(response)
throw new HTTPError(response, url, options)
}

const data: T = await response.json()
return data
return await response.json()
} catch (error) {
if (options.retries > 0 && (error as HTTPError)?.status === 500) {
if (options.retries > 0 && (error as HTTPError).status === 500) {
await wait(500)
return request<T>(url, { ...options, retries: options.retries - 1 })
}

await (error as HTTPError).buildAdditionalDetails?.()

throw error
}
}
180 changes: 180 additions & 0 deletions src/request.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import {
describe,
it,
expect,
vi,
beforeAll,
beforeEach,
afterAll,
type Mock,
} from 'vitest'
import { config } from './config.js'
import type { SDKBaseConfig } from './types/index.js'
import { request } from './request.js'
import type { HTTPError } from './utils/index.js'
import { ValidationError } from './utils/index.js'
import type { ExtendedRequestInit } from './types/request.js'

const mockUrl = 'https://some.endpoint.com'
const mockSuccessMessage = { message: 'it worked!' }

const setUpMocks = (
mockConfig: SDKBaseConfig = {
userId: 'user-id',
integrator: 'mock-integrator',
widgetVersion: 'mock-widget-version',
apiKey: 'mock-apikey',
} as SDKBaseConfig,
mockResponse: Response = {
ok: true,
status: 200,
statusText: 'Success',
json: () => Promise.resolve(mockSuccessMessage),
} as Response
) => {
;(global.fetch as Mock).mockResolvedValue(mockResponse)

vi.spyOn(config, 'get').mockReturnValue(mockConfig)
}

describe('request', () => {
beforeAll(() => {
vi.spyOn(global, 'fetch')
})

beforeEach(() => {
;(global.fetch as Mock).mockReset()
})

afterAll(() => {
vi.clearAllMocks()
})

it('should be able to successfully make a fetch request', async () => {
setUpMocks()

const response = await request<{ message: string }>(mockUrl)

expect(response).toEqual(mockSuccessMessage)
})

it('should remove the extended request init properties that fetch does not care about', async () => {
setUpMocks()

const options: ExtendedRequestInit = {
retries: 0,
skipTrackingHeaders: true,
disableLiFiErrorCodes: true,
headers: {
'x-lifi-integrator': 'mock-integrator',
},
}

const response = await request<{ message: string }>(mockUrl, options)

expect(response).toEqual(mockSuccessMessage)

const fetchOptions = (global.fetch as Mock).mock.calls[0][1]

expect(fetchOptions).toEqual({
headers: {
'x-lifi-integrator': 'mock-integrator',
},
})
})

it('should update the headers information available from config', async () => {
setUpMocks()

await request<{ message: string }>('https://some.endpoint.com')

const url = (global.fetch as Mock).mock.calls[0][0]
const headers = (global.fetch as Mock).mock.calls[0][1].headers

expect(url).toEqual(mockUrl)

expect(headers['x-lifi-api-key']).toEqual('mock-apikey')
expect(headers['x-lifi-integrator']).toEqual('mock-integrator')
expect(headers['x-lifi-sdk']).toBeDefined()
expect(headers['x-lifi-userid']).toEqual('user-id')
expect(headers['x-lifi-widget']).toEqual('mock-widget-version')
})

it('should not add tracking headers when option is skipTrackingHeaders set to true', async () => {
setUpMocks()

await request<{ message: string }>('https://some.endpoint.com', {
skipTrackingHeaders: true,
})

const url = (global.fetch as Mock).mock.calls[0][0]
const headers = (global.fetch as Mock).mock.calls[0][1].headers

expect(url).toEqual(mockUrl)

expect(headers).toBeUndefined()
})

describe('when dealing with errors', () => {
it('should throw an error if the Integrator property is missing from the config', async () => {
const mockConfig = {
userId: 'user-id',
widgetVersion: 'mock-widget-version',
apiKey: 'mock-apikey',
} as SDKBaseConfig
setUpMocks(mockConfig)

await expect(
request<{ message: string }>('https://some.endpoint.com', {
skipTrackingHeaders: true,
})
).rejects.toThrowError(
new ValidationError(
'You need to provide the Integrator property. Please see documentation https://docs.li.fi/integrate-li.fi-js-sdk/set-up-the-sdk'
)
)
})
it('should throw a error with when the request fails', async () => {
expect.assertions(2)

const mockResponse = {
ok: false,
status: 400,
statusText: 'Bad Request',
json: () => Promise.resolve({ message: 'it broke' }),
} as Response
setUpMocks(undefined, mockResponse)

try {
await request<{ message: string }>('https://some.endpoint.com', {
skipTrackingHeaders: true,
})
} catch (e) {
expect((e as HTTPError).name).toEqual('HTTPError')
expect((e as HTTPError).status).toEqual(400)
}
})
it('should throw a error and attempt retries when the request fails with a 500', async () => {
expect.assertions(3)

const mockResponse = {
ok: false,
status: 500,
statusText: 'Internal Server Error',
json: () => Promise.resolve({ message: 'it broke' }),
} as Response
setUpMocks(undefined, mockResponse)

try {
await request<{ message: string }>('https://some.endpoint.com', {
skipTrackingHeaders: true,
retries: 3,
})
} catch (e) {
expect((e as HTTPError).name).toEqual('HTTPError')
expect((e as HTTPError).status).toEqual(500)
expect(global.fetch as Mock).toBeCalledTimes(4)
}
})
})
})
Loading
Loading