File tree Expand file tree Collapse file tree 2 files changed +12
-11
lines changed Expand file tree Collapse file tree 2 files changed +12
-11
lines changed Original file line number Diff line number Diff line change @@ -211,15 +211,16 @@ struct GenerativeAIService {
211
211
}
212
212
213
213
private func httpResponse( urlResponse: URLResponse) throws -> HTTPURLResponse {
214
- // Verify the status code is 200
214
+ // The following condition should always be true: "Whenever you make HTTP URL load requests, any
215
+ // response objects you get back from the URLSession, NSURLConnection, or NSURLDownload class
216
+ // are instances of the HTTPURLResponse class."
215
217
guard let response = urlResponse as? HTTPURLResponse else {
216
218
VertexLog . error (
217
219
code: . generativeAIServiceNonHTTPResponse,
218
220
" Response wasn't an HTTP response, internal error \( urlResponse) "
219
221
)
220
- throw NSError (
221
- domain: " com.google.generative-ai " ,
222
- code: - 1 ,
222
+ throw URLError (
223
+ . badServerResponse,
223
224
userInfo: [ NSLocalizedDescriptionKey: " Response was not an HTTP response. " ]
224
225
)
225
226
}
@@ -229,14 +230,11 @@ struct GenerativeAIService {
229
230
230
231
private func jsonData( jsonText: String) throws -> Data {
231
232
guard let data = jsonText. data ( using: . utf8) else {
232
- let error = NSError (
233
- domain: " com.google.generative-ai " ,
234
- code: - 1 ,
235
- userInfo: [ NSLocalizedDescriptionKey: " Could not parse response as UTF8. " ]
236
- )
237
- throw error
233
+ throw DecodingError . dataCorrupted ( DecodingError . Context (
234
+ codingPath: [ ] ,
235
+ debugDescription: " Could not parse response as UTF8. "
236
+ ) )
238
237
}
239
-
240
238
return data
241
239
}
242
240
Original file line number Diff line number Diff line change @@ -740,6 +740,9 @@ final class GenerativeModelTests: XCTestCase {
740
740
return
741
741
}
742
742
XCTAssertEqual ( underlyingError. localizedDescription, " Response was not an HTTP response. " )
743
+ let underlyingNSError = underlyingError as NSError
744
+ XCTAssertEqual ( underlyingNSError. domain, NSURLErrorDomain)
745
+ XCTAssertEqual ( underlyingNSError. code, URLError . Code. badServerResponse. rawValue)
743
746
}
744
747
745
748
func testGenerateContent_failure_invalidResponse( ) async throws {
You can’t perform that action at this time.
0 commit comments