Skip to content

Commit c763e96

Browse files
committed
refactoring error handling
1 parent 3367c8a commit c763e96

File tree

4 files changed

+65
-63
lines changed

4 files changed

+65
-63
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.1")
16+
.package(url: "https://github.com/The-Igor/async-http-client.git", from: "1.4.2")
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: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import retry_policy_service
1414
public struct ReplicateAPI{
1515

1616
/// Client type alias
17-
public typealias ReplicateClient = Http.Proxy<ReplicateAPI.JsonReader,JsonWriter>
17+
public typealias ReplicateClient = Http.Proxy<JsonReader,JsonWriter>
1818

1919
/// Communication layer
2020
private let client : ReplicateClient
@@ -75,10 +75,13 @@ public struct ReplicateAPI{
7575
public func getCollections(collection_slug : String) async throws -> CollectionOfModels{
7676

7777
let path = "collections/\(collection_slug)"
78-
let rule = [Http.Validate.status(.range(200..<500))]
79-
let result : Http.Response<CollectionOfModels> = try await client.get(path: path, validate: rule)
80-
81-
return result.value
78+
let rule = [Http.Validate.status(.range(200..<299))]
79+
do{
80+
let result : Http.Response<CollectionOfModels> = try await client.get(path: path, validate: rule)
81+
return result.value
82+
}catch{
83+
throw ResponseError.check(error)
84+
}
8285
}
8386

8487
/// Get a model
@@ -88,10 +91,14 @@ public struct ReplicateAPI{
8891
public func getModel(owner: String, name: String) async throws -> Model{
8992

9093
let path = "models/\(owner)/\(name)"
91-
let rule = [Http.Validate.status(.range(200..<500))]
92-
let result : Http.Response<Model> = try await client.get(path: path, validate: rule)
94+
let rule = [Http.Validate.status(.range(200..<299))]
95+
do{
96+
let result : Http.Response<Model> = try await client.get(path: path, validate: rule)
97+
return result.value
98+
}catch{
99+
throw ResponseError.check(error)
100+
}
93101

94-
return result.value
95102
}
96103

97104
/// Create prediction
@@ -141,13 +148,18 @@ public struct ReplicateAPI{
141148
by id : String
142149
) async throws -> Prediction<Output>{
143150

144-
let rule = [Http.Validate.status(.range(200..<500))]
145-
let result : Http.Response<Prediction<Output>> = try await client.get(
146-
path: "predictions/\(id)",
147-
validate: rule
148-
)
149-
150-
return result.value
151+
let rule = [Http.Validate.status(.range(200..<299))]
152+
153+
do{
154+
let result : Http.Response<Prediction<Output>> = try await client.get(
155+
path: "predictions/\(id)",
156+
validate: rule
157+
)
158+
159+
return result.value
160+
}catch{
161+
throw ResponseError.check(error)
162+
}
151163
}
152164

153165
// MARK: - Private
@@ -196,21 +208,26 @@ public struct ReplicateAPI{
196208
with body : HttpBody<Input>
197209
) async throws -> Prediction<Output>{
198210

199-
let rule = [Http.Validate.status(.range(200..<500))]
200-
let result : Http.Response<Prediction<Output>> = try await client.post(
201-
path: "predictions",
202-
body : body,
203-
validate: rule
204-
)
211+
let rule = [Http.Validate.status(.range(200..<299))]
205212

206-
return result.value
213+
do{
214+
let result : Http.Response<Prediction<Output>> = try await client.post(
215+
path: "predictions",
216+
body : body,
217+
validate: rule
218+
)
219+
220+
return result.value
221+
}catch{
222+
throw ResponseError.check(error)
223+
}
207224
}
208225
}
209226

210227
// MARK: - File private -
211228

212229
/// Client configuration type alias
213-
fileprivate typealias ClientConfig = Http.Configuration<ReplicateAPI.JsonReader,JsonWriter>
230+
fileprivate typealias ClientConfig = Http.Configuration<JsonReader,JsonWriter>
214231

215232
/// Client configuration
216233
/// - Parameter endpoint: Replicate endpoint
@@ -220,7 +237,7 @@ fileprivate func clientCfg(baseURL: URL, apiKey: String)
220237
let session = URLSession(configuration: sessionCfg(apiKey))
221238

222239
return .init(
223-
reader: ReplicateAPI.JsonReader(),
240+
reader: JsonReader(),
224241
writer: JsonWriter(),
225242
baseURL: baseURL,
226243
session: session)

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Foundation
9+
import async_http_client
910

1011
/// Error format : "{\"detail\":\"Incorrect authentication token. Learn how to authenticate and get your API token here: https://replicate.com/docs/reference/http#authentication\"}"
1112
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@@ -44,4 +45,26 @@ public struct ResponseError: Hashable, CustomStringConvertible, LocalizedError,
4445
}
4546
}
4647

48+
static func check(_ error : Error) -> Error{
49+
50+
guard case let Http.Errors.status(status, _, data) = error else{
51+
return error
52+
}
53+
54+
guard let status, (400...499).contains(status) else{
55+
return error
56+
}
57+
58+
guard let data = data else{
59+
return error
60+
}
61+
62+
if let error = try? JSONDecoder().decode(ResponseError.self, from: data){
63+
return ReplicateAPI.Errors.read(error)
64+
}
65+
66+
return error
67+
68+
}
69+
4770
}

Sources/replicate-kit-swift/net/reader/JsonReader.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)