Skip to content

Commit

Permalink
chore: handle decoding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisabhash committed Jul 1, 2024
1 parent b22ef51 commit 8f3b75a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions HostApp/HostApp/Views/ExampleLivenessView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct ExampleLivenessView: View {
viewModel.presentationState = .error(.countdownFaceTooClose)
case .failure(.invalidSignature):
viewModel.presentationState = .error(.invalidSignature)
case .failure(.runtimeError):
viewModel.presentationState = .error(.runtimeError)
default:
viewModel.presentationState = .liveness
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public struct FaceLivenessDetectionError: Error, Equatable {
message: "The signature on the request is invalid.",
recoverySuggestion: "Ensure the device time is correct and try again."
)

public static let runtimeError = FaceLivenessDetectionError(
code: 18,
message: "An unexpected runtime error occurred.",
recoverySuggestion: "Please try again."
)

public static func == (lhs: FaceLivenessDetectionError, rhs: FaceLivenessDetectionError) -> Bool {
lhs.code == rhs.code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public struct FaceLivenessDetectorView: View {
isPreviewScreenEnabled: !disableStartView
)
)

faceDetector.setFaceDetectionSessionConfigurationWrapper(configuration: viewModel)
}

init(
Expand Down Expand Up @@ -143,6 +141,22 @@ public struct FaceLivenessDetectorView: View {
throw FaceLivenessDetectionError.accessDenied
}
}
DispatchQueue.main.async {
if let faceDetector = viewModel.faceDetector as? FaceDetectorShortRange.Model {
faceDetector.setFaceDetectionSessionConfigurationWrapper(configuration: viewModel)
}
}
}
.onReceive(viewModel.$livenessState) { output in
switch output.state {
case .encounteredUnrecoverableError(let error):
let closeCode = error.webSocketCloseCode ?? .normalClosure
viewModel.livenessService?.closeSocket(with: closeCode)
isPresented = false
onCompletion(.failure(mapError(error)))
default:
break
}
}
case .awaitingLivenessSession(let challenge):
Color.clear
Expand All @@ -157,7 +171,6 @@ public struct FaceLivenessDetectorView: View {
}
}
}

case .displayingGetReadyView(let challenge):
GetReadyPageView(
onBegin: {
Expand Down Expand Up @@ -219,6 +232,8 @@ public struct FaceLivenessDetectorView: View {
return .faceInOvalMatchExceededTimeLimitError
case .socketClosed:
return .socketClosed
case .runtimeError:
return .runtimeError
default:
return .cameraPermissionDenied
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ class FaceLivenessDetectionViewModel: ObservableObject {
self?.livenessState
.unrecoverableStateEncountered(.socketClosed)
}
case .runtimeError:
DispatchQueue.main.async {
self?.livenessState
.unrecoverableStateEncountered(.runtimeError)
}
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ struct LivenessStateMachine {
static let timedOut = LivenessError(code: 4, webSocketCloseCode: .ovalFitMatchTimeout)
static let couldNotOpenStream = LivenessError(code: 5, webSocketCloseCode: .unexpectedRuntimeError)
static let socketClosed = LivenessError(code: 6, webSocketCloseCode: .normalClosure)
static let runtimeError = LivenessError(code: 7, webSocketCloseCode: .unexpectedRuntimeError)
static let viewResignation = LivenessError(code: 8, webSocketCloseCode: .viewClosure)

static func == (lhs: LivenessError, rhs: LivenessError) -> Bool {
Expand Down

0 comments on commit 8f3b75a

Please sign in to comment.