Skip to content

Commit a8a0184

Browse files
committed
feat: replace unexpected error
1 parent fab9dbe commit a8a0184

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

Sources/VRCKit/APIClient.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final actor APIClient {
4343
/// - Throws: `VRCKitError.unexpectedError` if the username and password cannot be converted to UTF-8 data.
4444
private func encodeAuthorization(_ credential: Credential) throws -> String {
4545
guard let payload = credential.authString.data(using: .utf8) else {
46-
throw VRCKitError.unexpected
46+
throw VRCKitError.credentialNotSet
4747
}
4848
return "Basic \(payload.base64EncodedString())"
4949
}
@@ -108,7 +108,7 @@ public final actor APIClient {
108108
return
109109
}
110110
guard let data = data, let reponse = urlResponse as? HTTPURLResponse else {
111-
continuation.resume(throwing: VRCKitError.invalidResponse)
111+
continuation.resume(throwing: VRCKitError.invalidResponse(String(describing: data)))
112112
return
113113
}
114114
continuation.resume(returning: (data, reponse))
@@ -121,7 +121,7 @@ public final actor APIClient {
121121
private func requestWithFoundation(_ request: URLRequest) async throws -> HTTPResponse {
122122
let (data, response) = try await URLSession.shared.data(for: request)
123123
guard let response = response as? HTTPURLResponse else {
124-
throw VRCKitError.invalidResponse
124+
throw VRCKitError.invalidResponse(String(describing: data))
125125
}
126126
return (data, response)
127127
}

Sources/VRCKit/Errors.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public enum VRCKitError: Error, LocalizedError, Equatable {
2121
/// Represents an error indicating that the client has been deallocated.
2222
case clientDeallocated
2323

24+
/// Represents an error indicating that credential not set.
25+
case credentialNotSet
26+
2427
/// Represents an error indicating an invalid response was received.
25-
case invalidResponse
28+
case invalidResponse(_ details: String)
2629

2730
/// Represents an error indicating an invalid request with additional details.
2831
case invalidRequest(_ details: String)
2932

3033
/// Represents an error indicating an authentication failure.
3134
case unauthorized
3235

33-
/// Represents an unexpected error.
34-
case unexpected
35-
3636
/// Represents an url error.
3737
case urlError
3838

@@ -42,21 +42,21 @@ public enum VRCKitError: Error, LocalizedError, Equatable {
4242
case .apiError: "API Error"
4343
case .badGateway: "Bad Gateway"
4444
case .clientDeallocated: "Client Deallocated"
45+
case .credentialNotSet: "Credential Error"
4546
case .invalidResponse: "Invalid Response"
4647
case .invalidRequest: "Invalid Request"
4748
case .unauthorized: "Unauthorized"
48-
case .unexpected: "Unexpected"
4949
case .urlError: "URL Error"
5050
}
5151
}
5252

5353
/// Provides a localized failure reason for the error.
5454
public var failureReason: String? {
5555
switch self {
56-
case .apiError(let details), .invalidRequest(let details):
57-
details
58-
default:
59-
errorDescription
56+
case .apiError(let details): details
57+
case .invalidRequest(let details): details
58+
case .invalidResponse(let details): details
59+
default: errorDescription
6060
}
6161
}
6262
}

Sources/VRCKit/Services/AuthenticationService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final actor AuthenticationService: APIService, AuthenticationServiceProto
3535
} catch _ as DecodingError {
3636
let result: RequiresTwoFactorAuthResponse = try Serializer.shared.decode(response.data)
3737
guard let requires = result.requires else {
38-
throw VRCKitError.unexpected
38+
throw VRCKitError.invalidResponse("\(result.requires?.rawValue)")
3939
}
4040
return .right(requires)
4141
}

Sources/VRCKit/Utils/Serializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class Serializer: Sendable {
3535
do {
3636
let errorResponse = try decoder.decode(ErrorResponse.self, from: data)
3737
if errorResponse.error.statusCode == 401 {
38-
throw VRCKitError.unauthorized
38+
throw VRCKitError.credentialNotSet
3939
} else {
4040
throw VRCKitError.apiError(errorResponse.error.message)
4141
}

0 commit comments

Comments
 (0)