Skip to content

Commit 93b5f89

Browse files
committed
status code handling
1 parent ebafb50 commit 93b5f89

File tree

4 files changed

+34
-56
lines changed

4 files changed

+34
-56
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
targets: ["replicate-kit-swift"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/The-Igor/async-http-client.git", from: "1.4.2")
16+
.package(url: "https://github.com/The-Igor/async-http-client.git", from: "1.4.3")
1717
],
1818
targets: [
1919
// Targets are the basic building blocks of a package. A target can define a module or a test suite.

Sources/replicate-kit-swift/ReplicateAPI.swift

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ public struct ReplicateAPI{
7676

7777
let path = "collections/\(collection_slug)"
7878

79-
do{
80-
return try await client.get(path: path, validate: rule).value
81-
}catch{
82-
throw ResponseError.check(error)
83-
}
79+
return try await client.get(path: path, validate: rule).value
8480
}
8581

8682
/// Get a model
@@ -91,11 +87,7 @@ public struct ReplicateAPI{
9187

9288
let path = "models/\(owner)/\(name)"
9389

94-
do{
95-
return try await client.get(path: path, validate: rule).value
96-
}catch{
97-
throw ResponseError.check(error)
98-
}
90+
return try await client.get(path: path, validate: rule).value
9991

10092
}
10193

@@ -146,14 +138,7 @@ public struct ReplicateAPI{
146138
by id : String
147139
) async throws -> Prediction<Output>{
148140

149-
do{
150-
return try await client.get(
151-
path: "predictions/\(id)",
152-
validate: rule
153-
).value
154-
}catch{
155-
throw ResponseError.check(error)
156-
}
141+
try await client.get(path: "predictions/\(id)", validate: rule).value
157142
}
158143

159144
// MARK: - Private
@@ -202,15 +187,11 @@ public struct ReplicateAPI{
202187
with body : HttpBody<Input>
203188
) async throws -> Prediction<Output>{
204189

205-
do{
206-
return try await client.post(
207-
path: "predictions",
208-
body : body,
209-
validate: rule
210-
).value
211-
}catch{
212-
throw ResponseError.check(error)
213-
}
190+
try await client.post(
191+
path: "predictions",
192+
body : body,
193+
validate: rule
194+
).value
214195
}
215196
}
216197

@@ -250,4 +231,4 @@ fileprivate func sessionCfg (_ token : String) -> URLSessionConfiguration{
250231
}
251232

252233
/// Between the range responses from the server is valid
253-
fileprivate let rule = [Http.Validate.status(.range(200..<299))]
234+
fileprivate let rule = [Http.Validate.status(.check(errorFn))]

Sources/replicate-kit-swift/error/Errors.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ extension ReplicateAPI{
2727

2828
/// Could not decode error format response
2929
case couldNotDecodeErrorContainer
30-
31-
/// Could not decode error description
32-
case read(ResponseError)
3330
}
3431
}

Sources/replicate-kit-swift/error/ResponseError.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,30 @@ public struct ResponseError: Hashable, CustomStringConvertible, LocalizedError,
4646
}
4747

4848

49-
/// Processing cases server cannot or will not process the request due to something that is perceived to be a client error
50-
/// 401 Unauthorized
51-
/// 402 Payment Required
52-
/// - Parameter error: Error
53-
/// - Returns: Error with a decription why server cannot or will not process the request or Error
54-
static func check(_ error : Error) -> Error{
55-
56-
guard case let Http.Errors.status(status, _, data) = error else{
57-
return error
58-
}
59-
60-
guard let status, (400...499).contains(status) else{
61-
return error
62-
}
63-
64-
guard let data = data else{
65-
return error
66-
}
67-
68-
if let error = try? JSONDecoder().decode(ResponseError.self, from: data){
69-
return ReplicateAPI.Errors.read(error)
70-
}
71-
49+
50+
}
51+
52+
/// Processing cases server cannot or will not process the request due to something that is perceived to be a client error
53+
/// 401 Unauthorized
54+
/// 402 Payment Required
55+
/// - Parameter error: Error
56+
/// - Returns: Error with a decription why server cannot or will not process the request if it managed to parse data response, standard status error or nil if requst was succesful
57+
internal func errorFn(status: Int, _ response : URLResponse, data: Data?) -> Error?{
58+
59+
if (200...299).contains(status) { return nil }
60+
61+
guard (400...499).contains(status) else{
62+
return Http.Errors.status(status, response, data)
63+
}
64+
65+
guard let data = data else{
66+
return Http.Errors.status(status, response, data)
67+
}
68+
69+
if let error = try? JSONDecoder().decode(ResponseError.self, from: data){
7270
return error
73-
7471
}
72+
73+
return Http.Errors.status(status, response, data)
74+
7575
}

0 commit comments

Comments
 (0)