Skip to content

Commit ebaef74

Browse files
authored
chore(liveness): improve documentation for error handling (#6634)
1 parent e00d4b9 commit ebaef74

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed

docs/src/pages/[platform]/connected-components/liveness/QuickStartReact.jsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ export function LivenessQuickStartReact() {
66
const [loading, setLoading] = React.useState(true);
77
const [createLivenessApiData, setCreateLivenessApiData] =
88
React.useState(null);
9-
10-
React.useEffect(() => {
11-
const fetchCreateLiveness = async () => {
9+
const fetchCreateLiveness = async () => {
1210
/*
1311
* This should be replaced with a real call to your own backend API
1412
*/
@@ -18,8 +16,9 @@ export function LivenessQuickStartReact() {
1816

1917
setCreateLivenessApiData(data);
2018
setLoading(false);
21-
};
19+
};
2220

21+
React.useEffect(() => {
2322
fetchCreateLiveness();
2423
}, []);
2524

@@ -46,6 +45,24 @@ export function LivenessQuickStartReact() {
4645
}
4746
};
4847

48+
// Use a ref to track if we're currently handling an error
49+
const isHandlingError = React.useRef(false);
50+
51+
const handleError = async (error) => {
52+
console.error('Liveness error:', error);
53+
54+
// Simple infinite loop prevention
55+
if (isHandlingError.current) return;
56+
isHandlingError.current = true;
57+
setLoading(true);
58+
59+
// Create a new session for retry - sessions are single-use
60+
await fetchCreateLiveness();
61+
62+
// Reset error handling flag
63+
isHandlingError.current = false;
64+
};
65+
4966
return (
5067
<ThemeProvider>
5168
{loading ? (
@@ -55,9 +72,7 @@ export function LivenessQuickStartReact() {
5572
sessionId={createLivenessApiData.sessionId}
5673
region="us-east-1"
5774
onAnalysisComplete={handleAnalysisComplete}
58-
onError={(error) => {
59-
console.error(error);
60-
}}
75+
onError={handleError}
6176
/>
6277
)}
6378
</ThemeProvider>

docs/src/pages/[platform]/connected-components/liveness/QuickStartReact.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ export function LivenessQuickStartReact() {
88
sessionId: string;
99
} | null>(null);
1010

11-
React.useEffect(() => {
12-
const fetchCreateLiveness: () => Promise<void> = async () => {
13-
/*
14-
* This should be replaced with a real call to your own backend API
15-
*/
16-
await new Promise((r) => setTimeout(r, 2000));
17-
const mockResponse = { sessionId: 'mockSessionId' };
18-
const data = mockResponse;
19-
20-
setCreateLivenessApiData(data);
21-
setLoading(false);
22-
};
11+
const fetchCreateLiveness = async () => {
12+
/*
13+
* This should be replaced with a real call to your own backend API
14+
*/
15+
await new Promise((r) => setTimeout(r, 2000));
16+
const mockResponse = { sessionId: 'mockSessionId' };
17+
const data = mockResponse;
18+
setCreateLivenessApiData(data);
19+
setLoading(false);
20+
};
2321

22+
React.useEffect(() => {
2423
fetchCreateLiveness();
2524
}, []);
2625

@@ -47,6 +46,24 @@ export function LivenessQuickStartReact() {
4746
}
4847
};
4948

49+
// Use a ref to track if we're currently handling an error
50+
const isHandlingError = React.useRef(false);
51+
52+
const handleError = async (error) => {
53+
console.error('Liveness error:', error);
54+
55+
// Simple infinite loop prevention
56+
if (isHandlingError.current) return;
57+
isHandlingError.current = true;
58+
setLoading(true);
59+
60+
// Create a new session for retry - sessions are single-use
61+
await fetchCreateLiveness();
62+
63+
// Reset error handling flag
64+
isHandlingError.current = false;
65+
};
66+
5067
return (
5168
<ThemeProvider>
5269
{loading ? (
@@ -56,9 +73,7 @@ export function LivenessQuickStartReact() {
5673
sessionId={createLivenessApiData.sessionId}
5774
region="us-east-1"
5875
onAnalysisComplete={handleAnalysisComplete}
59-
onError={(error) => {
60-
console.error(error);
61-
}}
76+
onError={handleError}
6277
/>
6378
)}
6479
</ThemeProvider>

docs/src/pages/[platform]/connected-components/liveness/quick-start-pull.react.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,7 @@ Once a valid sessionId, region, and callback has been passed, FaceLivenessDetect
213213
```
214214
</Tabs.Panel>
215215
</Tabs.Container>
216+
217+
#### Error handling
218+
219+
When errors occur during liveness detection, It is required to create a new session before allowing users to retry. Session IDs are single-use and cannot be reused after an error.

0 commit comments

Comments
 (0)