Skip to content

Commit 3929c3d

Browse files
feat(recognize): handle recoverable-error, recognition-failure, and non-cancelable events from Keyless SDK v3
1 parent 0d316df commit 3929c3d

5 files changed

Lines changed: 50 additions & 24 deletions

File tree

packages/recognize/src/lib/classes/recognize-error.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { RecognizeErrorCode } from '../defs/recognize-error-code.js';
33
/** @public */
44
export class RecognizeError extends Error {
55
code: RecognizeErrorCode;
6+
// recoverable: boolean;
67

78
constructor(code: RecognizeErrorCode, options?: ErrorOptions) {
89
super(RecognizeErrorCode[code], options);
910

1011
this.code = code;
1112
this.name = 'RecognizeError';
13+
// this.recoverable = false;
1214
}
1315
}

packages/recognize/src/lib/defs/recognize-error-code.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export enum RecognizeErrorCode {
3434
CORE_USER_ALREADY_ENROLLED = 3002,
3535
CORE_USER_NOT_ENROLLED = 3003,
3636
CORE_FACE_NOT_MATCHING = 3004,
37-
CORE_NOT_ENOUGH_CIRCUITS = 3005,
3837
CORE_SECRET_NOT_FOUND = 3006,
3938
CORE_USER_LOCKED_OUT = 3007,
4039
CORE_CUSTOMER_NOT_FOUND = 3008,
@@ -44,10 +43,7 @@ export enum RecognizeErrorCode {
4443
BIOM_LIVENESS_ENVIRONMENT_AWARE_NOT_SUPPORTED = 4003,
4544
BIOM_DEVICE_ENVIRONMENT_AWARE_NOT_SUPPORTED = 4004,
4645
SERVER_ERROR = 5000,
47-
SERVER_ENCRYPTION_FAILED = 5001,
4846
SERVER_RECOGNITION_FAILED = 5002,
49-
SERVER_AUTHORIZATION_FAILED = 5003,
50-
SERVER_TIMEOUT = 5004,
5147
SECURITY_ERROR = 6000,
5248
SECURITY_DEVICE_NOT_GENUINE = 6001,
5349
}

packages/recognize/src/lib/defs/recognize-sdk-to-recognize-proxy-error-map.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import { RecognizeErrorCode } from './recognize-error-code.js';
1111

1212
export const RECOGNIZE_SDK_TO_RECOGNIZE_PROXY_ERROR_MAP: Record<string, RecognizeErrorCode> =
1313
Object.freeze({
14+
FRAME_RESULTS_SET_UNSET: RecognizeErrorCode.SDK_ERROR,
1415
OPTIONS_UNSET: RecognizeErrorCode.SDK_ERROR,
16+
USER_LOCKOUT_EXPIRATION_UNSET: RecognizeErrorCode.SDK_ERROR,
1517
VIDEO_ELEMENT_UNSET: RecognizeErrorCode.SDK_ERROR,
16-
VIDEO_ELEMENT_EVENT_LISTENERS_UNSET: RecognizeErrorCode.SDK_ERROR,
1718
MEDIA_DEVICES_EMPTY_AUDIO_INPUT_LABEL: RecognizeErrorCode.SDK_ERROR,
1819
MEDIA_DEVICES_EMPTY_VIDEO_INPUT_LABEL: RecognizeErrorCode.CAMERA_PERMISSION_DENIED,
1920
MEDIA_DEVICES_NO_VIDEO_INPUTS: RecognizeErrorCode.CAMERA_NOT_FOUND,
@@ -25,36 +26,32 @@ export const RECOGNIZE_SDK_TO_RECOGNIZE_PROXY_ERROR_MAP: Record<string, Recogniz
2526
MEDIA_STREAM_OVERCONSTRAINED: RecognizeErrorCode.CAMERA_NOT_SUPPORTED,
2627
MEDIA_STREAM_SECURITY: RecognizeErrorCode.CAMERA_ERROR,
2728
MEDIA_STREAM_TYPE: RecognizeErrorCode.CAMERA_ERROR,
28-
MEDIA_STREAM_UNSET: RecognizeErrorCode.CAMERA_ERROR,
2929
SERVER_CUSTOMER_NOT_FOUND: RecognizeErrorCode.CORE_CUSTOMER_NOT_FOUND,
30-
SERVER_FACE_DOES_NOT_MATCH: RecognizeErrorCode.CORE_FACE_NOT_MATCHING,
31-
SERVER_FORBIDDEN: RecognizeErrorCode.SERVER_AUTHORIZATION_FAILED,
32-
SERVER_IMAGE_ENCRYPT_FAILED: RecognizeErrorCode.SERVER_ENCRYPTION_FAILED,
3330
SERVER_INTERNAL_ERROR: RecognizeErrorCode.SERVER_ERROR,
34-
SERVER_NO_ATTEMPTS_LEFT: RecognizeErrorCode.CORE_NOT_ENOUGH_CIRCUITS,
31+
SERVER_INVALID_REQUEST: RecognizeErrorCode.SERVER_ERROR,
32+
SERVER_INVALID_RESPONSE: RecognizeErrorCode.SERVER_ERROR,
3533
SERVER_RECOGNITION_FAILED: RecognizeErrorCode.SERVER_RECOGNITION_FAILED,
36-
SERVER_TIMEOUT: RecognizeErrorCode.SERVER_TIMEOUT,
37-
SERVER_UNAVAILABLE_SERVICE: RecognizeErrorCode.SERVER_ERROR,
38-
SERVER_UNPROCESSABLE_EVENT: RecognizeErrorCode.SERVER_ERROR,
39-
SERVER_USER_ALREADY_ENROLLED: RecognizeErrorCode.CORE_USER_ALREADY_ENROLLED,
40-
SERVER_USER_NOT_FOUND: RecognizeErrorCode.CORE_USER_NOT_ENROLLED,
4134
SERVER_USER_LOCKED_OUT: RecognizeErrorCode.CORE_USER_LOCKED_OUT,
42-
SERVER_VALIDATION_FAILED: RecognizeErrorCode.SERVER_ERROR,
35+
SESSION_DATABASE_ERROR: RecognizeErrorCode.SDK_ERROR,
4336
SESSION_MANAGER_NOT_NULL: RecognizeErrorCode.SDK_ERROR,
4437
SESSION_MANAGER_NULL: RecognizeErrorCode.SDK_ERROR,
38+
SESSION_MEDIA_STREAM_TIMEOUT: RecognizeErrorCode.SDK_TIMEOUT,
39+
SESSION_MEDIA_STREAM_UNSET: RecognizeErrorCode.SDK_ERROR,
4540
CREATE_MEDIA_STREAM_ARGS_UNSET: RecognizeErrorCode.SDK_ERROR,
4641
EXCEPTION: RecognizeErrorCode.SDK_ERROR,
42+
GET_VIDEO_MEDIA_DEVICES_ARGS_UNSET: RecognizeErrorCode.SDK_ERROR,
4743
RUNTIME_VIOLATION: RecognizeErrorCode.SECURITY_ERROR,
4844
SYMBOL_DESCRIPTION_UNSET: RecognizeErrorCode.SDK_ERROR,
49-
SESSION_ID_UNSET: RecognizeErrorCode.SDK_ERROR,
5045
CUSTOMER_UNSET: RecognizeErrorCode.SDK_INVALID_CONFIGURATION,
46+
SERVICE_URL_PARSE_FAILED: RecognizeErrorCode.SDK_INVALID_CONFIGURATION,
47+
SERVICE_URL_UNSET: RecognizeErrorCode.SDK_INVALID_CONFIGURATION,
5148
USERNAME_UNSET: RecognizeErrorCode.SDK_INVALID_CONFIGURATION,
5249
WEB_ASSEMBLY_ABORTED: RecognizeErrorCode.SDK_WEB_ASSEMBLY_ERROR,
5350
WEB_ASSEMBLY_FACTORY_FAILED: RecognizeErrorCode.SDK_WEB_ASSEMBLY_ERROR,
5451
WEB_ASSEMBLY_IMPORT_FAILED: RecognizeErrorCode.SDK_WEB_ASSEMBLY_IMPORT_FAILED,
5552
WEB_ASSEMBLY_NOT_READY: RecognizeErrorCode.SDK_WEB_ASSEMBLY_IMPORT_NOT_FULFILLED,
5653
WEB_ASSEMBLY_MODULE_NOT_FOUND: RecognizeErrorCode.SDK_WEB_ASSEMBLY_ERROR,
57-
QUEUE_UNSET: RecognizeErrorCode.SDK_ERROR,
5854
SLUG_UNSUPPORTED: RecognizeErrorCode.SDK_ERROR,
55+
QUEUE_UNSET: RecognizeErrorCode.SDK_ERROR,
5956
SYMBOL_UNSET: RecognizeErrorCode.SDK_ERROR,
6057
});

packages/recognize/src/lib/recognize.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { CAMERA_ONLY_DISABLE_STEPS } from './defs/constants.js';
1212
import { RecognizeErrorCode } from './defs/recognize-error-code.js';
1313
import { RECOGNIZE_SDK_TO_RECOGNIZE_PROXY_ERROR_MAP } from './defs/recognize-sdk-to-recognize-proxy-error-map.js';
1414
import type { KeylessSuccessEvent } from './recognize-sdk/index.js';
15+
import { KeylessRecoverableErrorEvent } from './recognize-sdk/index.js';
1516
import type {
1617
RecognizeWebComponent,
1718
RecognizeWebComponentClient,
@@ -45,7 +46,7 @@ export function recognize(
4546
}
4647
};
4748

48-
const addEventListeners = (element: RecognizeWebComponent): void => {
49+
const addEventListeners = (el: RecognizeWebComponent): void => {
4950
aborter = new AbortController();
5051

5152
const onEvent = (type: RecognizeWebComponentEvent['type']) => {
@@ -61,10 +62,36 @@ export function recognize(
6162
};
6263

6364
const onError = (e: ErrorEvent) => {
65+
const recoverable = e instanceof KeylessRecoverableErrorEvent;
66+
67+
if (recoverable) {
68+
console.log('%c[recognize] recoverable-error → calling element.dispose()', 'color: orange; font-weight: bold');
69+
element?.dispose();
70+
}
71+
6472
const code: RecognizeErrorCode =
65-
RECOGNIZE_SDK_TO_RECOGNIZE_PROXY_ERROR_MAP[e.error.message] ?? RecognizeErrorCode.SDK_ERROR;
73+
RECOGNIZE_SDK_TO_RECOGNIZE_PROXY_ERROR_MAP[e.error?.message] ??
74+
RecognizeErrorCode.SDK_ERROR;
6675

6776
const error: RecognizeError = new RecognizeError(code, { cause: e.error });
77+
// error.recoverable = recoverable;
78+
79+
for (const observer of observers) {
80+
observer.error?.(error);
81+
}
82+
83+
observers.clear();
84+
};
85+
86+
const onRecoverableError = (e: KeylessRecoverableErrorEvent) => onError(e);
87+
88+
const onRecognitionFailure = (e: CustomEvent) => {
89+
const error: RecognizeError = new RecognizeError(
90+
RecognizeErrorCode.SERVER_RECOGNITION_FAILED,
91+
{
92+
cause: e.detail,
93+
},
94+
);
6895

6996
for (const observer of observers) {
7097
observer.error?.(error);
@@ -75,10 +102,13 @@ export function recognize(
75102

76103
const options: AddEventListenerOptions = { signal: aborter.signal };
77104

78-
element.addEventListener('error', onError, options);
79-
element.addEventListener('step-change', onEvent('step-change'), options);
80-
element.addEventListener('success', onSuccess, options);
81-
element.addEventListener('video-frame-quality', onEvent('video-frame-quality'), options);
105+
el.addEventListener('error', onError, options);
106+
el.addEventListener('recoverable-error', onRecoverableError, options);
107+
el.addEventListener('recognition-failure', onRecognitionFailure, options);
108+
el.addEventListener('non-cancelable', onEvent('non-cancelable'), options);
109+
el.addEventListener('step-change', onEvent('step-change'), options);
110+
el.addEventListener('success', onSuccess, options);
111+
el.addEventListener('video-frame-quality', onEvent('video-frame-quality'), options);
82112
};
83113

84114
const setAttributes = (element: RecognizeWebComponent): void => {

packages/recognize/src/lib/recognize.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export type RecognizeWebComponent = KeylessAuthElement | KeylessEnrollElement;
8585

8686
/** @public */
8787
export type RecognizeWebComponentEvent =
88+
| { type: 'non-cancelable' }
8889
| { type: 'step-change'; detail: RecognizeWebComponentStepChangeEventDetail }
8990
| { type: 'video-frame-quality'; detail: RecognizeWebComponentVideoFrameQualityEventDetail };
9091

0 commit comments

Comments
 (0)