Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4ca27e

Browse files
committedDec 16, 2024·
review
1 parent c9fbb1b commit a4ca27e

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed
 

‎Coder Desktop/Coder Desktop/Preview Content/PreviewClient.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SwiftUI
2+
import Alamofire
23

34
struct PreviewClient: Client {
45
init(url _: URL, token _: String? = nil) {}
@@ -22,7 +23,7 @@ struct PreviewClient: Client {
2223
roles: []
2324
)
2425
} catch {
25-
throw ClientError.badResponse
26+
throw ClientError.reqError(AFError.explicitlyCancelled)
2627
}
2728
}
2829
}

‎Coder Desktop/Coder Desktop/SDK/Client.swift

+16-21
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ struct CoderClient: Client {
3535
parameters: body,
3636
headers: headers
3737
).serializingData().response
38-
guard let response = out.response else {
39-
throw ClientError.noResponse
40-
}
4138
switch out.result {
4239
case .success(let data):
43-
return HTTPResponse(resp: response, data: data, req: out.request)
44-
case .failure:
45-
throw ClientError.badResponse
40+
return HTTPResponse(resp: out.response!, data: data, req: out.request)
41+
case .failure(let error):
42+
throw ClientError.reqError(error)
4643
}
4744
}
4845

@@ -57,28 +54,26 @@ struct CoderClient: Client {
5754
method: method,
5855
headers: headers
5956
).serializingData().response
60-
guard let response = out.response else {
61-
throw ClientError.noResponse
62-
}
6357
switch out.result {
6458
case .success(let data):
65-
return HTTPResponse(resp: response, data: data, req: out.request)
66-
case .failure:
67-
throw ClientError.badResponse
59+
return HTTPResponse(resp: out.response!, data: data, req: out.request)
60+
case .failure(let error):
61+
throw ClientError.reqError(error)
6862
}
6963
}
7064

71-
func responseAsError(_ resp: HTTPResponse) throws(ClientError) -> APIError {
65+
func responseAsError(_ resp: HTTPResponse) -> ClientError {
7266
do {
7367
let body = try CoderClient.decoder.decode(Response.self, from: resp.data)
74-
return APIError(
68+
let out = APIError(
7569
response: body,
7670
statusCode: resp.resp.statusCode,
7771
method: resp.req?.httpMethod,
7872
url: resp.req?.url
7973
)
74+
return ClientError.apiError(out)
8075
} catch {
81-
throw ClientError.badResponse
76+
return ClientError.unexpectedResponse(resp.data[...1024])
8277
}
8378
}
8479

@@ -130,17 +125,17 @@ struct ValidationError: Decodable {
130125

131126
enum ClientError: Error {
132127
case apiError(APIError)
133-
case badResponse
134-
case noResponse
128+
case reqError(AFError)
129+
case unexpectedResponse(Data)
135130

136131
var description: String {
137132
switch self {
138133
case .apiError(let error):
139134
return error.description
140-
case .badResponse:
141-
return "Bad response"
142-
case .noResponse:
143-
return "No response"
135+
case .reqError(let error):
136+
return error.localizedDescription
137+
case .unexpectedResponse(let data):
138+
return "Unexpected response: \(data)"
144139
}
145140
}
146141
}

‎Coder Desktop/Coder Desktop/SDK/User.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ extension CoderClient {
44
func user(_ ident: String) async throws(ClientError) -> User {
55
let res = try await request("/api/v2/users/\(ident)", method: .get)
66
guard res.resp.statusCode == 200 else {
7-
let error = try responseAsError(res)
8-
throw ClientError.apiError(error)
7+
throw responseAsError(res)
98
}
109
do {
1110
return try CoderClient.decoder.decode(User.self, from: res.data)
1211
} catch {
13-
throw ClientError.badResponse
12+
throw ClientError.unexpectedResponse(res.data[...1024])
1413
}
1514
}
1615
}

‎Coder Desktop/Coder DesktopTests/Util.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct MockClient: Client {
6969
struct MockErrorClient: Client {
7070
init(url: URL, token: String?) {}
7171
func user(_ ident: String) async throws(ClientError) -> Coder_Desktop.User {
72-
throw ClientError.badResponse
72+
throw ClientError.reqError(.explicitlyCancelled)
7373
}
7474
}
7575

0 commit comments

Comments
 (0)
Please sign in to comment.