-
Notifications
You must be signed in to change notification settings - Fork 70
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
DNR500
wants to merge
6
commits into
main
Choose a base branch
from
handling-backend-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f12055a
chore: first pass at a rework ofthe backend errors code
DNR500 82f96fe
fix: utils index file with correct import
DNR500 5a92a13
test: adjust tests to new implementation
DNR500 c1be06d
chore: allow disabling of creation lifi error codes and error types
DNR500 0f7a124
chore: clean up unused code
DNR500 ed49b8d
chore: update after discussion
DNR500 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.