From a93130ee091221e4c494713f30fc1e7a0c45959d Mon Sep 17 00:00:00 2001 From: pdurlej Date: Sun, 17 May 2026 21:29:12 +0200 Subject: [PATCH] refactor: use ForgejoKit error categories (#31) Problem - SessionRestoreError now duplicates HTTP status-code classification that ForgejoKit exposes in 0.6.0. Change - Uses ForgejoKit's httpErrorCategory/httpStatusCode for auth and service restore failures. - Keeps the same user-facing SessionRestoreError outcomes. - Leaves network and certificate handling unchanged. Tests - DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild -quiet -project Forji/Forji.xcodeproj -scheme Forji -destination "platform=iOS Simulator,name=iPhone 17 Pro,OS=26.4.1" build-for-testing -only-testing:ForjiTests/SessionRestoreErrorTests - DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild -quiet -project Forji/Forji.xcodeproj -scheme Forji -destination "platform=iOS Simulator,name=iPhone 17 Pro,OS=26.4.1" test-without-building -only-testing:ForjiTests/SessionRestoreErrorTests Co-authored-by: Piotr Durlej Reviewed-on: https://codeberg.org/secana/Forji/pulls/31 --- .../Services/AuthenticationService.swift | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/Forji/Forji/Services/AuthenticationService.swift b/Forji/Forji/Services/AuthenticationService.swift index 05cbb70..d59a82c 100644 --- a/Forji/Forji/Services/AuthenticationService.swift +++ b/Forji/Forji/Services/AuthenticationService.swift @@ -257,18 +257,14 @@ enum SessionRestoreError: LocalizedError, Equatable { private static func fromAuthenticationError(_ error: AuthenticationError) -> SessionRestoreError { switch error { - case .invalidCredentials, .otpRequired: - .tokenExpired - case .serverNotFound: - .serverNotFound case .certificateError: .certificateError case .invalidResponse: .invalidServerResponse - case let .unknownError(statusCode): - fromHTTPStatusCode(statusCode) case .invalidURL: .serverNotFound + case .invalidCredentials, .otpRequired, .serverNotFound, .unknownError: + fromHTTPErrorCategory(error.httpErrorCategory, statusCode: error.httpStatusCode) } } @@ -278,8 +274,8 @@ enum SessionRestoreError: LocalizedError, Equatable { .serverNotFound case .invalidResponse, .decodingFailed: .invalidServerResponse - case let .httpError(statusCode, _): - fromHTTPStatusCode(statusCode) + case .httpError: + fromHTTPErrorCategory(error.httpErrorCategory, statusCode: error.httpStatusCode) case .noActiveInstance: .tokenValidationFailed case .notMergeable, .mergeConflict: @@ -287,18 +283,27 @@ enum SessionRestoreError: LocalizedError, Equatable { } } - private static func fromHTTPStatusCode(_ statusCode: Int) -> SessionRestoreError { - switch statusCode { - case 401: + private static func fromHTTPErrorCategory( + _ category: HTTPErrorCategory?, + statusCode: Int?, + ) -> SessionRestoreError { + switch category { + case .authentication: .tokenExpired - case 403: + case .permissionDenied: .tokenPermissionDenied - case 404: + case .notFound: .serverNotFound - case 500 ... 599: + case .server: .serverUnavailable - default: - .tokenValidationFailedHTTPStatus(statusCode) + case .other: + if let statusCode { + .tokenValidationFailedHTTPStatus(statusCode) + } else { + .tokenValidationFailed + } + case nil: + .tokenValidationFailed } } }