Skip to content

Commit 782e940

Browse files
authored
fix: throw generic HTTPError (#368)
1 parent f6cb964 commit 782e940

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

Sources/Auth/Internal/APIClient.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ struct APIClient: Sendable {
3333
/// There are some GoTrue endpoints that can return a `PostgrestError`, for example the
3434
/// ``AuthAdmin/deleteUser(id:shouldSoftDelete:)`` that could return an error in case the
3535
/// user is referenced by other schemas.
36-
let postgrestError = try configuration.decoder.decode(
36+
if let postgrestError = try? configuration.decoder.decode(
3737
PostgrestError.self,
3838
from: response.data
39-
)
40-
throw postgrestError
39+
) {
40+
throw postgrestError
41+
}
42+
43+
throw HTTPError(data: response.data, response: response.response)
4144
}
4245

4346
return response

Sources/PostgREST/PostgrestBuilder.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ public class PostgrestBuilder: @unchecked Sendable {
110110
let response = try await http.fetch(request, baseURL: configuration.url)
111111

112112
guard 200 ..< 300 ~= response.statusCode else {
113-
let error = try configuration.decoder.decode(PostgrestError.self, from: response.data)
114-
throw error
113+
if let error = try? configuration.decoder.decode(PostgrestError.self, from: response.data) {
114+
throw error
115+
}
116+
117+
throw HTTPError(data: response.data, response: response.response)
115118
}
116119

117120
let value = try decode(response.data)

Sources/Storage/StorageApi.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ public class StorageApi: @unchecked Sendable {
3333

3434
let response = try await http.rawFetch(request)
3535
guard (200 ..< 300).contains(response.statusCode) else {
36-
let error = try configuration.decoder.decode(StorageError.self, from: response.data)
37-
throw error
36+
if let error = try? configuration.decoder.decode(StorageError.self, from: response.data) {
37+
throw error
38+
}
39+
40+
throw HTTPError(data: response.data, response: response.response)
3841
}
3942

4043
return response
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// HTTPError.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 07/05/24.
6+
//
7+
8+
import Foundation
9+
10+
#if canImport(FoundationNetworking)
11+
import FoundationNetworking
12+
#endif
13+
14+
/// A generic error from a HTTP request.
15+
///
16+
/// Contains both the `Data` and `HTTPURLResponse` which you can use to extract more information about it.
17+
public struct HTTPError: Error, Sendable {
18+
public let data: Data
19+
public let response: HTTPURLResponse
20+
21+
public init(data: Data, response: HTTPURLResponse) {
22+
self.data = data
23+
self.response = response
24+
}
25+
}

0 commit comments

Comments
 (0)