diff --git a/Sources/VRCKit/APIClient.swift b/Sources/VRCKit/APIClient.swift index f272b65..5f5be28 100644 --- a/Sources/VRCKit/APIClient.swift +++ b/Sources/VRCKit/APIClient.swift @@ -43,7 +43,7 @@ public final actor APIClient { /// - Throws: `VRCKitError.unexpectedError` if the username and password cannot be converted to UTF-8 data. private func encodeAuthorization(_ credential: Credential) throws -> String { guard let payload = credential.authString.data(using: .utf8) else { - throw VRCKitError.unexpected + throw VRCKitError.credentialNotSet } return "Basic \(payload.base64EncodedString())" } @@ -108,7 +108,7 @@ public final actor APIClient { return } guard let data = data, let reponse = urlResponse as? HTTPURLResponse else { - continuation.resume(throwing: VRCKitError.invalidResponse) + continuation.resume(throwing: VRCKitError.invalidResponse(String(describing: data))) return } continuation.resume(returning: (data, reponse)) @@ -121,7 +121,7 @@ public final actor APIClient { private func requestWithFoundation(_ request: URLRequest) async throws -> HTTPResponse { let (data, response) = try await URLSession.shared.data(for: request) guard let response = response as? HTTPURLResponse else { - throw VRCKitError.invalidResponse + throw VRCKitError.invalidResponse(String(describing: data)) } return (data, response) } diff --git a/Sources/VRCKit/Errors.swift b/Sources/VRCKit/Errors.swift index 66894ec..cfcc4ca 100644 --- a/Sources/VRCKit/Errors.swift +++ b/Sources/VRCKit/Errors.swift @@ -21,8 +21,11 @@ public enum VRCKitError: Error, LocalizedError, Equatable { /// Represents an error indicating that the client has been deallocated. case clientDeallocated + /// Represents an error indicating that credential not set. + case credentialNotSet + /// Represents an error indicating an invalid response was received. - case invalidResponse + case invalidResponse(_ details: String) /// Represents an error indicating an invalid request with additional details. case invalidRequest(_ details: String) @@ -30,9 +33,6 @@ public enum VRCKitError: Error, LocalizedError, Equatable { /// Represents an error indicating an authentication failure. case unauthorized - /// Represents an unexpected error. - case unexpected - /// Represents an url error. case urlError @@ -42,10 +42,10 @@ public enum VRCKitError: Error, LocalizedError, Equatable { case .apiError: "API Error" case .badGateway: "Bad Gateway" case .clientDeallocated: "Client Deallocated" + case .credentialNotSet: "Credential Error" case .invalidResponse: "Invalid Response" case .invalidRequest: "Invalid Request" case .unauthorized: "Unauthorized" - case .unexpected: "Unexpected" case .urlError: "URL Error" } } @@ -53,10 +53,10 @@ public enum VRCKitError: Error, LocalizedError, Equatable { /// Provides a localized failure reason for the error. public var failureReason: String? { switch self { - case .apiError(let details), .invalidRequest(let details): - details - default: - errorDescription + case .apiError(let details): details + case .invalidRequest(let details): details + case .invalidResponse(let details): details + default: errorDescription } } } diff --git a/Sources/VRCKit/Services/AuthenticationService.swift b/Sources/VRCKit/Services/AuthenticationService.swift index 086142c..5662185 100644 --- a/Sources/VRCKit/Services/AuthenticationService.swift +++ b/Sources/VRCKit/Services/AuthenticationService.swift @@ -30,7 +30,7 @@ public final actor AuthenticationService: APIService, AuthenticationServiceProto } catch _ as DecodingError { let result: RequiresTwoFactorAuthResponse = try Serializer.shared.decode(response.data) guard let requires = result.requires else { - throw VRCKitError.unexpected + throw VRCKitError.invalidResponse("\(result.requires?.rawValue)") } return .right(requires) } diff --git a/Sources/VRCKit/Utils/Serializer.swift b/Sources/VRCKit/Utils/Serializer.swift index 451ac6c..174d192 100644 --- a/Sources/VRCKit/Utils/Serializer.swift +++ b/Sources/VRCKit/Utils/Serializer.swift @@ -35,7 +35,7 @@ final class Serializer: Sendable { do { let errorResponse = try decoder.decode(ErrorResponse.self, from: data) if errorResponse.error.statusCode == 401 { - throw VRCKitError.unauthorized + throw VRCKitError.credentialNotSet } else { throw VRCKitError.apiError(errorResponse.error.message) }