Skip to content

Commit

Permalink
chore(liveness): export error types (#4450)
Browse files Browse the repository at this point in the history
* chore(liveness): export error types

* Create thin-experts-destroy.md

* fix unti tests

* update error types

* chore: switch to union type only

* Update packages/react-liveness/src/components/FaceLivenessDetector/service/utils/liveness.ts

Co-authored-by: Caleb Pollman <[email protected]>

---------

Co-authored-by: Ioana Brooks <[email protected]>
Co-authored-by: Caleb Pollman <[email protected]>
  • Loading branch information
3 people authored Sep 19, 2023
1 parent 6a5a4d7 commit 8ca0450
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-experts-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-react-liveness": patch
---

chore(liveness): export error types
2 changes: 1 addition & 1 deletion packages/react-liveness/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 80,
functions: 83,
functions: 82,
lines: 89,
statements: 89,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export {
AwsCredentialProvider,
AwsTemporaryCredentials,
AwsCredentials,
ErrorState,
} from './service';
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
IlluminationState,
StreamActorCallback,
LivenessError,
ErrorState,
} from '../types';
import {
BlazeFaceFaceDetection,
Expand Down Expand Up @@ -717,7 +718,7 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
// callbacks
callUserPermissionDeniedCallback: assign({
errorState: (context, event) => {
let errorState: LivenessErrorState;
let errorState: ErrorState;

if ((event.data!.message as string).includes('15 fps')) {
errorState = LivenessErrorState.CAMERA_FRAMERATE_ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
* The liveness error states
*/

export enum LivenessErrorState {
TIMEOUT = 'TIMEOUT',
RUNTIME_ERROR = 'RUNTIME_ERROR',
FRESHNESS_TIMEOUT = 'FRESHNESS_TIMEOUT',
SERVER_ERROR = 'SERVER_ERROR',
CAMERA_FRAMERATE_ERROR = 'CAMERA_FRAMERATE_ERROR',
CAMERA_ACCESS_ERROR = 'CAMERA_ACCESS_ERROR',
FACE_DISTANCE_ERROR = 'FACE_DISTANCE_ERROR',
MOBILE_LANDSCAPE_ERROR = 'MOBILE_LANDSCAPE_ERROR',
MULTIPLE_FACES_ERROR = 'MULTIPLE_FACES_ERROR',
}
export const LivenessErrorState = {
TIMEOUT: 'TIMEOUT',
RUNTIME_ERROR: 'RUNTIME_ERROR',
FRESHNESS_TIMEOUT: 'FRESHNESS_TIMEOUT',
SERVER_ERROR: 'SERVER_ERROR',
CAMERA_FRAMERATE_ERROR: 'CAMERA_FRAMERATE_ERROR',
CAMERA_ACCESS_ERROR: 'CAMERA_ACCESS_ERROR',
FACE_DISTANCE_ERROR: 'FACE_DISTANCE_ERROR',
MOBILE_LANDSCAPE_ERROR: 'MOBILE_LANDSCAPE_ERROR',
MULTIPLE_FACES_ERROR: 'MULTIPLE_FACES_ERROR',
} as const;

export type ErrorState = keyof typeof LivenessErrorState;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AwsCredentialProvider } from './credentials';
import { LivenessErrorState } from './error';
import { ErrorState } from './error';

/**
* The props for the FaceLivenessDetectorCore which allows for full configuration of auth
Expand Down Expand Up @@ -124,6 +124,6 @@ export enum FaceMatchState {
}

export interface LivenessError {
state: LivenessErrorState;
state: ErrorState;
error: Error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
LivenessOvalDetails,
IlluminationState,
} from './liveness';
import { LivenessErrorState } from './error';
import { ErrorState } from './error';
import {
VideoRecorder,
LivenessStreamProvider,
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface HydratedLivenessContext {
ovalAssociatedParams: OvalAssociatedParams;
faceMatchAssociatedParams: FaceMatchAssociatedParams;
freshnessColorAssociatedParams: FreshnessColorAssociatedParams;
errorState: LivenessErrorState;
errorState: ErrorState;
livenessStreamProvider: LivenessStreamProvider;
responseStreamActorRef: ActorRef<any>;
shouldDisconnect: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FaceMatchState,
BoundingBox,
LivenessErrorState,
ErrorState,
} from '../types';
import { FaceDetection } from '../types/faceDetection';
import { ClientFreshnessColorSequence } from '../types/service';
Expand Down Expand Up @@ -459,7 +460,7 @@ export function isCameraDeviceVirtual(device: MediaDeviceInfo): boolean {
return device.label.toLowerCase().includes('virtual');
}

export const LivenessErrorStateStringMap: Record<LivenessErrorState, string> = {
export const LivenessErrorStateStringMap = {
[LivenessErrorState.RUNTIME_ERROR]: 'RUNTIME_ERROR',
[LivenessErrorState.SERVER_ERROR]: 'SERVER_ERROR',
[LivenessErrorState.TIMEOUT]: 'TIMEOUT',
Expand Down Expand Up @@ -741,18 +742,13 @@ export async function isFaceDistanceBelowThreshold({
isMobile?: boolean;
}): Promise<{
isDistanceBelowThreshold: boolean;
error?:
| LivenessErrorState.FACE_DISTANCE_ERROR
| LivenessErrorState.MULTIPLE_FACES_ERROR;
error?: ErrorState;
}> {
const detectedFaces = await faceDetector.detectFaces(videoEl);
let detectedFace: Face;

let isDistanceBelowThreshold = false;
let error:
| LivenessErrorState.FACE_DISTANCE_ERROR
| LivenessErrorState.MULTIPLE_FACES_ERROR
| undefined;
let error: ErrorState | undefined;

switch (detectedFaces.length) {
case 0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { Flex, Button, Text } from '@aws-amplify/ui-react';
import { AlertIcon } from '@aws-amplify/ui-react/internal';

import { LivenessErrorState } from '../service';
import { LivenessErrorState, ErrorState } from '../service';

import { Toast } from './Toast';
import { Overlay } from './Overlay';
Expand All @@ -21,7 +21,7 @@ export interface FaceLivenessErrorModalProps {
}

const renderToastErrorModal = (props: {
error: LivenessErrorState;
error: ErrorState;
displayText: Required<ErrorDisplayText>;
}) => {
const { error: errorState, displayText } = props;
Expand Down Expand Up @@ -80,7 +80,7 @@ export const renderErrorModal = ({
errorState,
overrideErrorDisplayText,
}: {
errorState: LivenessErrorState;
errorState: ErrorState;
overrideErrorDisplayText?: ErrorDisplayText;
}): JSX.Element | null => {
const displayText: Required<ErrorDisplayText> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';

import { LivenessErrorState } from '../../service';
import { LivenessErrorState, ErrorState } from '../../service';
import {
FaceLivenessErrorModal,
renderErrorModal,
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('FaceLivenessErrorModal', () => {
render(
<FaceLivenessErrorModal onRetry={() => {}}>
{renderErrorModal({
errorState: errorState as unknown as LivenessErrorState,
errorState: errorState as unknown as ErrorState,
})}
</FaceLivenessErrorModal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IlluminationState,
FaceMatchState,
LivenessErrorState,
ErrorState,
} from '../../service';

import {
Expand Down Expand Up @@ -33,7 +34,7 @@ describe('Hint', () => {
};
const mockActorSend = jest.fn();

let errorState: LivenessErrorState | null = null;
let errorState: ErrorState | null = null;
let faceMatchState: FaceMatchState | null = null;
let illuminationState: IlluminationState | null = null;
let faceMatchStateBeforeStart: FaceMatchState | null = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';

import { LivenessErrorState } from '../../service';
import { ErrorState, LivenessErrorState } from '../../service';
import {
FaceLivenessErrorModal,
renderErrorModal,
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('FaceLivenessErrorModal', () => {
render(
<FaceLivenessErrorModal onRetry={() => {}}>
{renderErrorModal({
errorState: errorState as unknown as LivenessErrorState,
errorState: errorState as unknown as ErrorState,
})}
</FaceLivenessErrorModal>
);
Expand Down
1 change: 1 addition & 0 deletions packages/react-liveness/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export {
AwsCredentialProvider,
AwsCredentials,
AwsTemporaryCredentials,
ErrorState,
} from './components';

0 comments on commit 8ca0450

Please sign in to comment.