Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
}
Loading