diff --git a/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandler.swift b/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandler.swift index 3ea5f91e..eb0e6bed 100644 --- a/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandler.swift +++ b/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandler.swift @@ -59,11 +59,11 @@ struct ResponseHandler: ResponseHandlerProtocol { } } - func handleNetworkError(_ error: AFError, statusCode: Int?) throws { + func handleNetworkError(_ error: AFError, statusCode: Int?, responseType: Any.Type) throws { if let underlyingError = error.underlyingError as? URLError { try handleURLError(underlyingError) } else { - try handleStatusCodeError(statusCode) + try handleStatusCodeError(statusCode, responseType: responseType) } } @@ -78,18 +78,22 @@ struct ResponseHandler: ResponseHandlerProtocol { } } - func handleStatusCodeError(_ statusCode: Int?) throws { + func handleStatusCodeError(_ statusCode: Int?, responseType: Any.Type) throws { switch statusCode ?? -1 { case 400: throw SmartIdError.incorrectParameters case 401: throw SmartIdError.invalidAccessRights case 404: - throw SmartIdError.accountNotFound + throw responseType == SmartIdSessionResponse.self ? + SmartIdError.sessionNotFound : + SmartIdError.accountNotFound case 409: throw SmartIdError.exceededUnsuccessfulRequests case 429: throw SmartIdError.tooManyRequests + case 471: + throw SmartIdError.notQualified case 480: throw SmartIdError.oldApi case 580: diff --git a/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandlerProtocol.swift b/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandlerProtocol.swift index f1f335ea..1f581bbd 100644 --- a/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandlerProtocol.swift +++ b/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Handler/ResponseHandlerProtocol.swift @@ -25,7 +25,7 @@ public protocol ResponseHandlerProtocol: Sendable { func handleSessionResponse(_ responseValue: Any) throws func handleSessionResult(_ response: SmartIdSessionStatusResponseCode) throws func handleCancellationError(_ error: Error) throws - func handleNetworkError(_ error: AFError, statusCode: Int?) throws + func handleNetworkError(_ error: AFError, statusCode: Int?, responseType: Any.Type) throws func handleURLError(_ error: URLError) throws - func handleStatusCodeError(_ statusCode: Int?) throws + func handleStatusCodeError(_ statusCode: Int?, responseType: Any.Type) throws } diff --git a/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Request/RequestPerformer.swift b/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Request/RequestPerformer.swift index 6cca51de..f294f024 100644 --- a/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Request/RequestPerformer.swift +++ b/Modules/SmartIdLib/Sources/SmartIdLib/Networking/Request/RequestPerformer.swift @@ -81,7 +81,11 @@ struct RequestPerformer: RequestPerfomerProtocol, Loggable { case .failure(let afError): continuation.resume(with: Result { try responseHandler.handleCancellationError(afError) - try responseHandler.handleNetworkError(afError, statusCode: response.response?.statusCode) + try responseHandler.handleNetworkError( + afError, + statusCode: response.response?.statusCode, + responseType: T.self + ) throw SmartIdError.generalError }) diff --git a/Modules/SmartIdLib/Tests/SmartIdLibTests/Networking/Handler/ResponseHandlerTests.swift b/Modules/SmartIdLib/Tests/SmartIdLibTests/Networking/Handler/ResponseHandlerTests.swift index fc5784a6..b43ceda8 100644 --- a/Modules/SmartIdLib/Tests/SmartIdLibTests/Networking/Handler/ResponseHandlerTests.swift +++ b/Modules/SmartIdLib/Tests/SmartIdLibTests/Networking/Handler/ResponseHandlerTests.swift @@ -165,7 +165,7 @@ struct ResponseHandlerTests { let afError = AFError.sessionTaskFailed(error: urlError) #expect(throws: SmartIdError.noInternetConnection) { - try handler.handleNetworkError(afError, statusCode: nil) + try handler.handleNetworkError(afError, statusCode: nil, responseType: SmartIdSessionIdResponse.self) } } @@ -175,7 +175,7 @@ struct ResponseHandlerTests { let afError = AFError.sessionTaskFailed(error: urlError) #expect(throws: SmartIdError.timeout) { - try handler.handleNetworkError(afError, statusCode: nil) + try handler.handleNetworkError(afError, statusCode: nil, responseType: SmartIdSessionIdResponse.self) } } @@ -184,7 +184,7 @@ struct ResponseHandlerTests { let afError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 401)) #expect(throws: SmartIdError.invalidAccessRights) { - try handler.handleNetworkError(afError, statusCode: 401) + try handler.handleNetworkError(afError, statusCode: 401, responseType: SmartIdSessionIdResponse.self) } } @@ -193,7 +193,34 @@ struct ResponseHandlerTests { let afError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 400)) #expect(throws: SmartIdError.incorrectParameters) { - try handler.handleNetworkError(afError, statusCode: 400) + try handler.handleNetworkError(afError, statusCode: 400, responseType: SmartIdSessionIdResponse.self) + } + } + + @Test + func handleNetworkError_throwsNotQualifiedWhenUnacceptableStatusCode471Returned() { + let afError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 471)) + + #expect(throws: SmartIdError.notQualified) { + try handler.handleNetworkError(afError, statusCode: 471, responseType: SmartIdSessionIdResponse.self) + } + } + + @Test + func handleNetworkError_throwsAccountNotFoundWhenStatusCode404ReturnedForSignatureRequest() { + let afError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)) + + #expect(throws: SmartIdError.accountNotFound) { + try handler.handleNetworkError(afError, statusCode: 404, responseType: SmartIdSessionIdResponse.self) + } + } + + @Test + func handleNetworkError_throwsSessionNotFoundWhenStatusCode404ReturnedForSessionStatusRequest() { + let afError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)) + + #expect(throws: SmartIdError.sessionNotFound) { + try handler.handleNetworkError(afError, statusCode: 404, responseType: SmartIdSessionResponse.self) } } @@ -202,7 +229,7 @@ struct ResponseHandlerTests { let afError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 999)) #expect(throws: SmartIdError.technicalError) { - try handler.handleNetworkError(afError, statusCode: 999) + try handler.handleNetworkError(afError, statusCode: 999, responseType: SmartIdSessionIdResponse.self) } } }