Skip to content

Commit

Permalink
feat: throw on error when calling "getData" manually
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Previously "getData" was returning "undefined" when error occurred
  • Loading branch information
TheUnderScorer committed Nov 1, 2022
1 parent 8779c86 commit 3df14fc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,16 @@ The `useVisitorData` hook also returns a `getData` method which can make an API

```jsx
// src/App.js
import React, { useState } from 'react';
import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-react'
import React, { useState } from "react";
import { useVisitorData } from "@fingerprintjs/fingerprintjs-pro-react";

function App() {
const {
isLoading,
error,
getData,
} = useVisitorData({tag: 'subscription-form'}, { immediate: false });
const [email, setEmail] = useState('')
getData
} = useVisitorData({tag: "subscription-form"}, { immediate: false });
const [email, setEmail] = useState("");

if (isLoading) {
return <div>Loading...</div>;
Expand All @@ -144,26 +144,29 @@ function App() {
<div>
<form
onSubmit={(e) => {
e.preventDefault()
getData().then((data) => {
if (data) {
// do something with the visitor data
// for example, append visitor data to the form data to send to your server
console.log(data)
}
})
e.preventDefault();
getData()
.then((data) => {
// do something with the visitor data
// for example, append visitor data to the form data to send to your server
console.log(data)
})
.catch((error) => {
// Handle error
})

}}
>
<label htmlFor='email'>Email:</label>
<label htmlFor="email">Email:</label>
<input
id='email'
type='email'
name='email'
id="email"
type="email"
name="email"
required
value={email}
onChange={(e) => setEmail(e.currentTarget.value)}
/>
<button type='submit'>Subscribe</button>
<button type="submit">Subscribe</button>
</form>
</div>
);
Expand All @@ -182,6 +185,10 @@ When you use FingerprintJS Pro, you pay for each API call. Our [best practices](

Some fields from the [extendedResult](https://dev.fingerprint.com/docs/js-agent#extendedresult) (e.g `ip` or `lastSeenAt`) might change for the same visitor. If you need to get the current data, it is recommended to pass `ignoreCache=true` inside [getData](#returned-object) function.

## Error handling

`getData` throws errors directly from our Pro Agent without changing them. [Read more about error handling.](https://dev.fingerprint.com/docs/js-agent#error-handling)

### API Reference

You can find API reference [here](https://fingerprintjs.github.io/fingerprintjs-pro-react/).
Expand Down
6 changes: 4 additions & 2 deletions __tests__/use-visitor-data.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,17 @@ describe('useVisitorData', () => {
})

it('should correctly pass errors from SPA library', async () => {
getVisitorData.mockRejectedValueOnce(new Error(ERROR_CLIENT_TIMEOUT))
getVisitorData.mockRejectedValue(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({
const promise = hook.result.current.getData({
ignoreCache: false,
})

await expect(promise).rejects.toThrow(ERROR_CLIENT_TIMEOUT)
})

expect(hook.result.current.error?.message).toBe(ERROR_CLIENT_TIMEOUT)
Expand Down
2 changes: 2 additions & 0 deletions src/use-visitor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export function useVisitorData<TExtended extends boolean>(
error.name = 'FPJSAgentError'

setState((state) => ({ ...state, data: undefined, error }))

throw error
} finally {
setState((state) => (state.isLoading ? { ...state, isLoading: false } : state))
}
Expand Down

0 comments on commit 3df14fc

Please sign in to comment.