refactor: use ForgejoKit error categories

This commit is contained in:
Piotr Durlej 2026-05-13 19:03:31 +02:00
parent 075669d87e
commit 1db69bfc77

View file

@ -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
}
}
}