Skip to content

Commit 8f3b75a

Browse files
committed
chore: handle decoding errors
1 parent b22ef51 commit 8f3b75a

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

HostApp/HostApp/Views/ExampleLivenessView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct ExampleLivenessView: View {
4545
viewModel.presentationState = .error(.countdownFaceTooClose)
4646
case .failure(.invalidSignature):
4747
viewModel.presentationState = .error(.invalidSignature)
48+
case .failure(.runtimeError):
49+
viewModel.presentationState = .error(.runtimeError)
4850
default:
4951
viewModel.presentationState = .liveness
5052
}

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ public struct FaceLivenessDetectionError: Error, Equatable {
125125
message: "The signature on the request is invalid.",
126126
recoverySuggestion: "Ensure the device time is correct and try again."
127127
)
128+
129+
public static let runtimeError = FaceLivenessDetectionError(
130+
code: 18,
131+
message: "An unexpected runtime error occurred.",
132+
recoverySuggestion: "Please try again."
133+
)
128134

129135
public static func == (lhs: FaceLivenessDetectionError, rhs: FaceLivenessDetectionError) -> Bool {
130136
lhs.code == rhs.code

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ public struct FaceLivenessDetectorView: View {
8282
isPreviewScreenEnabled: !disableStartView
8383
)
8484
)
85-
86-
faceDetector.setFaceDetectionSessionConfigurationWrapper(configuration: viewModel)
8785
}
8886

8987
init(
@@ -143,6 +141,22 @@ public struct FaceLivenessDetectorView: View {
143141
throw FaceLivenessDetectionError.accessDenied
144142
}
145143
}
144+
DispatchQueue.main.async {
145+
if let faceDetector = viewModel.faceDetector as? FaceDetectorShortRange.Model {
146+
faceDetector.setFaceDetectionSessionConfigurationWrapper(configuration: viewModel)
147+
}
148+
}
149+
}
150+
.onReceive(viewModel.$livenessState) { output in
151+
switch output.state {
152+
case .encounteredUnrecoverableError(let error):
153+
let closeCode = error.webSocketCloseCode ?? .normalClosure
154+
viewModel.livenessService?.closeSocket(with: closeCode)
155+
isPresented = false
156+
onCompletion(.failure(mapError(error)))
157+
default:
158+
break
159+
}
146160
}
147161
case .awaitingLivenessSession(let challenge):
148162
Color.clear
@@ -157,7 +171,6 @@ public struct FaceLivenessDetectorView: View {
157171
}
158172
}
159173
}
160-
161174
case .displayingGetReadyView(let challenge):
162175
GetReadyPageView(
163176
onBegin: {
@@ -219,6 +232,8 @@ public struct FaceLivenessDetectorView: View {
219232
return .faceInOvalMatchExceededTimeLimitError
220233
case .socketClosed:
221234
return .socketClosed
235+
case .runtimeError:
236+
return .runtimeError
222237
default:
223238
return .cameraPermissionDenied
224239
}

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class FaceLivenessDetectionViewModel: ObservableObject {
111111
self?.livenessState
112112
.unrecoverableStateEncountered(.socketClosed)
113113
}
114+
case .runtimeError:
115+
DispatchQueue.main.async {
116+
self?.livenessState
117+
.unrecoverableStateEncountered(.runtimeError)
118+
}
114119
}
115120
})
116121

Sources/FaceLiveness/Views/Liveness/LivenessStateMachine.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ struct LivenessStateMachine {
164164
static let timedOut = LivenessError(code: 4, webSocketCloseCode: .ovalFitMatchTimeout)
165165
static let couldNotOpenStream = LivenessError(code: 5, webSocketCloseCode: .unexpectedRuntimeError)
166166
static let socketClosed = LivenessError(code: 6, webSocketCloseCode: .normalClosure)
167+
static let runtimeError = LivenessError(code: 7, webSocketCloseCode: .unexpectedRuntimeError)
167168
static let viewResignation = LivenessError(code: 8, webSocketCloseCode: .viewClosure)
168169

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

0 commit comments

Comments
 (0)