Skip to content

Commit 6c96cdd

Browse files
authored
fix: service exception handling (#21)
1 parent 3ea7626 commit 6c96cdd

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

Package.resolved

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
targets: ["FaceLiveness"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/aws-amplify/amplify-swift", from: "2.8.0")
16+
.package(url: "https://github.com/aws-amplify/amplify-swift", from: "2.11.0")
1717
],
1818
targets: [
1919
.target(

Sources/FaceLiveness/Utilities/UserAgent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct UserAgentValues {
5757
swiftVersion: Swift().version(),
5858
unameMachine: Device.current.machine.replacingOccurrences(of: ",", with: "_"),
5959
locale: Locale.current.identifier,
60-
lib: "lib/amplify-ui-swift-face-liveness/0.1.16",
60+
lib: "lib/amplify-ui-swift-face-liveness/1.0.1",
6161
additionalMetadata: additionalMetadata
6262
)
6363
}

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionError.swift

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public struct FaceLivenessDetectionError: Error, Equatable {
1212
public let message: String
1313
public let recoverySuggestion: String
1414

15+
public static let unknown = FaceLivenessDetectionError(
16+
code: 0,
17+
message: "An unknown error occurred.",
18+
recoverySuggestion: "Please open an issue...."
19+
)
20+
1521
public static let sessionNotFound = FaceLivenessDetectionError(
1622
code: 1,
1723
message: "Session not found.",
@@ -78,10 +84,42 @@ public struct FaceLivenessDetectionError: Error, Equatable {
7884
recoverySuggestion: "Confirm that you are using a valid `region` and try again."
7985
)
8086

81-
public static let unknown = FaceLivenessDetectionError(
87+
public static let validation = FaceLivenessDetectionError(
8288
code: 12,
83-
message: "An unknown error occurred.",
84-
recoverySuggestion: "Please open an issue...."
89+
message: "The input fails to satisfy the constraints specified by the service.",
90+
recoverySuggestion: """
91+
Potential reasons inlcude: video quality or size is invalid, video container format not supported,
92+
video does not have enough information - no person detected in video, request couldn't be parsed or is invalid,
93+
session has expired or is invalid, S3 bucket is invalid/in another AWS region, KMS Key is invalid."
94+
"""
95+
)
96+
97+
public static let internalServer = FaceLivenessDetectionError(
98+
code: 13,
99+
message: "Unexpected error during processing of request.",
100+
recoverySuggestion: ""
101+
)
102+
103+
public static let throttling = FaceLivenessDetectionError(
104+
code: 14,
105+
message: "A request was denied due to request throttling.",
106+
recoverySuggestion: """
107+
Occurs when too many requests were made by a user (exceeding their service quota),
108+
the service isn't able to scale, or a service-wide throttling was done to
109+
recover from an operational event.
110+
"""
111+
)
112+
113+
public static let serviceQuotaExceeded = FaceLivenessDetectionError(
114+
code: 15,
115+
message: "Occurs when a request would cause a service quota to be exceeded.",
116+
recoverySuggestion: ""
117+
)
118+
119+
public static let serviceUnavailable = FaceLivenessDetectionError(
120+
code: 16,
121+
message: "Service-wide throttling to recover from an operational event or service is not able to scale.",
122+
recoverySuggestion: ""
85123
)
86124

87125
public static func == (lhs: FaceLivenessDetectionError, rhs: FaceLivenessDetectionError) -> Bool {

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public struct FaceLivenessDetectorView: View {
7878
faceInOvalMatching: faceInOvalStateMatching,
7979
captureSession: captureSession,
8080
videoChunker: videoChunker,
81-
closeButtonAction: {},
81+
closeButtonAction: { onCompletion(.failure(.userCancelled)) },
8282
sessionID: sessionID
8383
)
8484
)
@@ -213,6 +213,18 @@ fileprivate func map(detectionCompletion: @escaping (Result<Void, FaceLivenessDe
213213
detectionCompletion(.failure(.invalidRegion))
214214
case .failure(.accessDenied):
215215
detectionCompletion(.failure(.accessDenied))
216+
case .failure(.validation):
217+
detectionCompletion(.failure(.validation))
218+
case .failure(.internalServer):
219+
detectionCompletion(.failure(.internalServer))
220+
case .failure(.throttling):
221+
detectionCompletion(.failure(.throttling))
222+
case .failure(.serviceQuotaExceeded):
223+
detectionCompletion(.failure(.serviceQuotaExceeded))
224+
case .failure(.serviceUnavailable):
225+
detectionCompletion(.failure(.serviceUnavailable))
226+
case .failure(.sessionNotFound):
227+
detectionCompletion(.failure(.sessionNotFound))
216228
default:
217229
detectionCompletion(.failure(.unknown))
218230
}

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ class FaceLivenessDetectionViewModel: ObservableObject {
8282

8383
func registerServiceEvents() {
8484
livenessService.register(onComplete: { [weak self] reason in
85+
self?.stopRecording()
86+
8587
switch reason {
8688
case .disconnectionEvent:
87-
self?.captureSession.stopRunning()
88-
8989
DispatchQueue.main.async {
9090
self?.livenessState.complete()
9191
}

Tests/FaceLivenessTests/MockLivenessService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MockLivenessService {
1919
var onFreshnessEvent: (LivenessEvent<FreshnessEvent>, Date) -> Void = { _, _ in }
2020
var onVideoEvent: (LivenessEvent<VideoEvent>, Date) -> Void = { _, _ in }
2121
var onInitializeLivenessStream: (String, String) -> Void = { _, _ in }
22+
var onServiceException: (FaceLivenessSessionError) -> Void = { _ in }
2223
}
2324

2425
extension MockLivenessService: LivenessService {

0 commit comments

Comments
 (0)