Skip to content

Commit 607d322

Browse files
committed
Throw errors on HTTP errors
1 parent cad08f1 commit 607d322

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Sources/VehicleKit/APIs/VKBMWConnectedDriveAPI.swift

+8-10
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ public class VKBMWConnectedDriveAPI: VKVehicleAPIBase<VKBMWConnectedDriveAPI.Cre
55
case authenticatingWithoutCredentials
66
case cantEncodeRequestBody
77
case invalidAuthConfig
8-
case authResponseNotHTTP
9-
case authResponseMissingLocation(
10-
status: Int,
11-
headers: [AnyHashable: Any],
12-
responseData: Data,
13-
responseString: String?
14-
)
158
case cantDecodeAuthResponseLocation(location: String)
169
case authResponseMissingToken(location: String)
1710
case authResponseMissingExpires(location: String)
1811
case invalidHost
1912
case responseInvalid
2013
case invalidURL
14+
case invalidRedirectTo(redirectTo: String)
2115
}
2216

2317
public struct Credentials: Codable {
@@ -107,7 +101,7 @@ public class VKBMWConnectedDriveAPI: VKVehicleAPIBase<VKBMWConnectedDriveAPI.Cre
107101
throw APIError.cantEncodeRequestBody
108102
}
109103
let url = try pathToURL(path: authConfig.endpoints.authenticate)
110-
let response: AuthResponse = try await VKHTTP.request(
104+
let response: VKHTTP.Response<AuthResponse> = try await VKHTTP.request(
111105
url,
112106
method: "POST",
113107
body: body.data(using: .utf8),
@@ -118,8 +112,12 @@ public class VKBMWConnectedDriveAPI: VKVehicleAPIBase<VKBMWConnectedDriveAPI.Cre
118112
"Referer": "https://login.bmwusa.com/",
119113
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
120114
]
121-
).data
122-
return String(response.redirectTo.dropFirst("redirect_uri=".count))
115+
)
116+
let redirectTo = response.data.redirectTo
117+
if !redirectTo.starts(with: "redirect_uri=") {
118+
throw APIError.invalidRedirectTo(redirectTo: redirectTo)
119+
}
120+
return String(redirectTo.dropFirst("redirect_uri=".count))
123121
}
124122

125123
private func authenticate() async throws -> Session {

Sources/VehicleKit/VKHTTP.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ struct VKHTTP {
44
enum HTTPError: Error {
55
case incompleteResponse
66
case invalidURL
7+
case errorStatus(code: Int, body: String?, rawBody: Data)
78
}
89

910
struct Response<ResponseType: Codable> {
1011
let data: ResponseType
11-
let response: URLResponse
12+
let response: HTTPURLResponse
1213
}
1314

1415
static let jsonDecoder = JSONDecoder()
@@ -25,18 +26,25 @@ struct VKHTTP {
2526
request.setValue(header.value, forHTTPHeaderField: header.key)
2627
}
2728
request.httpBody = body
28-
let (data, response): (Data, URLResponse) = try await withCheckedThrowingContinuation { continuation in
29+
let (data, response): (Data, HTTPURLResponse) = try await withCheckedThrowingContinuation { continuation in
2930
let task = URLSession.shared.dataTask(with: request) { data, response, error in
3031
if let error = error {
3132
continuation.resume(throwing: error)
32-
} else if let data = data, let response = response {
33+
} else if let data = data, let response = response as? HTTPURLResponse {
3334
continuation.resume(returning: (data, response))
3435
} else {
3536
continuation.resume(throwing: HTTPError.incompleteResponse)
3637
}
3738
}
3839
task.resume()
3940
}
41+
if response.statusCode >= 400 {
42+
throw HTTPError.errorStatus(
43+
code: response.statusCode,
44+
body: String(decoding: data, as: UTF8.self),
45+
rawBody: data
46+
)
47+
}
4048
return Response(data: data, response: response)
4149
}
4250

0 commit comments

Comments
 (0)