Skip to content

Commit

Permalink
fix: use correct errors messages from pro agent
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Oct 31, 2022
1 parent 5c0dbd4 commit b2e1a7f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
16 changes: 16 additions & 0 deletions __tests__/use-visitor-data.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { actWait, createWrapper } from './helpers'
import { act } from 'react-dom/test-utils'
import { useState } from 'react'
import userEvent from '@testing-library/user-event'
import { ERROR_CLIENT_TIMEOUT } from '@fingerprintjs/fingerprintjs-pro'

const testData = {
visitorId: 'abcdef123456',
Expand Down Expand Up @@ -135,4 +136,19 @@ describe('useVisitorData', () => {
expect(getVisitorData).toHaveBeenNthCalledWith(1, { extendedResult: false }, undefined)
expect(getVisitorData).toHaveBeenNthCalledWith(2, { extendedResult: true }, undefined)
})

it('should correctly pass errors from SPA library', async () => {
getVisitorData.mockRejectedValueOnce(new Error(ERROR_CLIENT_TIMEOUT))

const wrapper = createWrapper()
const hook = renderHook(() => useVisitorData({ ignoreCache: true }, { immediate: false }), { wrapper })

await act(async () => {
await hook.result.current.getData({
ignoreCache: false,
})
})

expect(hook.result.current.error?.message).toBe(ERROR_CLIENT_TIMEOUT)
})
})
13 changes: 7 additions & 6 deletions src/use-visitor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback, useContext, useEffect, useState } from 'react'
import { GetOptions, VisitorData } from '@fingerprintjs/fingerprintjs-pro-spa'
import { usePrevious } from './utils/use-previous'
import deepEquals from 'fast-deep-equal'
import { toError } from './utils/to-error'

export type UseVisitorDataOptions<TExtended extends boolean> = GetOptions<TExtended> & Partial<GetDataOptions>

Expand Down Expand Up @@ -48,12 +49,12 @@ export function useVisitorData<TExtended extends boolean>(
)
setState((state) => ({ ...state, data: result, isLoading: false, error: undefined }))
return result
} catch (error) {
if (error instanceof Error) {
error.message = `${error.name}: ${error.message}`
error.name = 'FPJSAgentError'
setState((state) => ({ ...state, data: undefined, error: error as Error }))
}
} catch (unknownError) {
const error = toError(unknownError)

error.name = 'FPJSAgentError'

setState((state) => ({ ...state, data: undefined, error }))
} finally {
setState((state) => (state.isLoading ? { ...state, isLoading: false } : state))
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/to-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function toError(error: unknown) {
if (error instanceof Error) {
return error
}

return new Error(String(error))
}

0 comments on commit b2e1a7f

Please sign in to comment.