Skip to content

Commit

Permalink
Merge pull request #97 from makinosp/feature/improvement-errors
Browse files Browse the repository at this point in the history
feat: replace unexpected error
  • Loading branch information
makinosp authored Oct 31, 2024
2 parents d28dca6 + a8a0184 commit b2c6886
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Sources/VRCKit/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())"
}
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/VRCKit/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ 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)

/// Represents an error indicating an authentication failure.
case unauthorized

/// Represents an unexpected error.
case unexpected

/// Represents an url error.
case urlError

Expand All @@ -42,21 +42,21 @@ 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"
}
}

/// 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
}
}
}
2 changes: 1 addition & 1 deletion Sources/VRCKit/Services/AuthenticationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/VRCKit/Utils/Serializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b2c6886

Please sign in to comment.