mirror of
https://codeberg.org/secana/ForgejoKit.git
synced 2026-06-16 05:13:53 -07:00
Problem - Callers currently need to inspect raw status codes to distinguish authentication, permission, not-found, and server failures. Change - Adds a small HTTPErrorCategory for 401, 403, 404, and 5xx responses. - Exposes httpStatusCode and httpErrorCategory on ServiceError and AuthenticationError. - Keeps the existing AuthenticationError cases intact and centralizes status-to-auth-error mapping. Tests - DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift test Co-authored-by: Piotr Durlej <pdurlej@users.noreply.github.com> Reviewed-on: https://codeberg.org/secana/ForgejoKit/pulls/2 Reviewed-by: secana <secana@noreply.codeberg.org>
48 lines
2.5 KiB
Swift
48 lines
2.5 KiB
Swift
import Testing
|
|
@testable import ForgejoKit
|
|
|
|
struct ErrorMappingTests {
|
|
|
|
@Test func classifiesHTTPStatusCodes() {
|
|
#expect(HTTPErrorCategory(statusCode: 401) == .authentication)
|
|
#expect(HTTPErrorCategory(statusCode: 403) == .permissionDenied)
|
|
#expect(HTTPErrorCategory(statusCode: 404) == .notFound)
|
|
#expect(HTTPErrorCategory(statusCode: 500) == .server)
|
|
#expect(HTTPErrorCategory(statusCode: 503) == .server)
|
|
#expect(HTTPErrorCategory(statusCode: 400) == .other)
|
|
}
|
|
|
|
@Test func exposesServiceErrorStatusAndCategory() {
|
|
let forbidden = ServiceError.httpError(statusCode: 403, message: "missing scope")
|
|
#expect(forbidden.httpStatusCode == 403)
|
|
#expect(forbidden.httpErrorCategory == .permissionDenied)
|
|
|
|
let serverError = ServiceError.httpError(statusCode: 502)
|
|
#expect(serverError.httpStatusCode == 502)
|
|
#expect(serverError.httpErrorCategory == .server)
|
|
|
|
#expect(ServiceError.invalidURL.httpStatusCode == nil)
|
|
#expect(ServiceError.invalidURL.httpErrorCategory == nil)
|
|
}
|
|
|
|
@Test func mapsAuthenticationStatusCodes() {
|
|
#expect(AuthenticationError.from(statusCode: 401) == .invalidCredentials)
|
|
#expect(AuthenticationError.from(statusCode: 401, body: "OTP required") == .otpRequired)
|
|
#expect(AuthenticationError.from(statusCode: 403) == .unknownError(statusCode: 403))
|
|
#expect(AuthenticationError.from(statusCode: 404) == .serverNotFound)
|
|
#expect(AuthenticationError.from(statusCode: 500) == .unknownError(statusCode: 500))
|
|
#expect(AuthenticationError.from(statusCode: 418) == .unknownError(statusCode: 418))
|
|
}
|
|
|
|
@Test func exposesAuthenticationStatusAndCategory() {
|
|
#expect(AuthenticationError.invalidCredentials.httpStatusCode == 401)
|
|
#expect(AuthenticationError.invalidCredentials.httpErrorCategory == .authentication)
|
|
#expect(AuthenticationError.serverNotFound.httpStatusCode == 404)
|
|
#expect(AuthenticationError.serverNotFound.httpErrorCategory == .notFound)
|
|
#expect(AuthenticationError.unknownError(statusCode: 403).httpStatusCode == 403)
|
|
#expect(AuthenticationError.unknownError(statusCode: 403).httpErrorCategory == .permissionDenied)
|
|
#expect(AuthenticationError.unknownError(statusCode: 502).httpErrorCategory == .server)
|
|
#expect(AuthenticationError.certificateError.httpStatusCode == nil)
|
|
#expect(AuthenticationError.certificateError.httpErrorCategory == nil)
|
|
}
|
|
}
|