Skip to content

Commit

Permalink
fix: throw generic HTTPError (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed May 8, 2024
1 parent f6cb964 commit 782e940
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
9 changes: 6 additions & 3 deletions Sources/Auth/Internal/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ struct APIClient: Sendable {
/// There are some GoTrue endpoints that can return a `PostgrestError`, for example the
/// ``AuthAdmin/deleteUser(id:shouldSoftDelete:)`` that could return an error in case the
/// user is referenced by other schemas.
let postgrestError = try configuration.decoder.decode(
if let postgrestError = try? configuration.decoder.decode(
PostgrestError.self,
from: response.data
)
throw postgrestError
) {
throw postgrestError
}

throw HTTPError(data: response.data, response: response.response)
}

return response
Expand Down
7 changes: 5 additions & 2 deletions Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ public class PostgrestBuilder: @unchecked Sendable {
let response = try await http.fetch(request, baseURL: configuration.url)

guard 200 ..< 300 ~= response.statusCode else {
let error = try configuration.decoder.decode(PostgrestError.self, from: response.data)
throw error
if let error = try? configuration.decoder.decode(PostgrestError.self, from: response.data) {
throw error
}

throw HTTPError(data: response.data, response: response.response)
}

let value = try decode(response.data)
Expand Down
7 changes: 5 additions & 2 deletions Sources/Storage/StorageApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public class StorageApi: @unchecked Sendable {

let response = try await http.rawFetch(request)
guard (200 ..< 300).contains(response.statusCode) else {
let error = try configuration.decoder.decode(StorageError.self, from: response.data)
throw error
if let error = try? configuration.decoder.decode(StorageError.self, from: response.data) {
throw error
}

throw HTTPError(data: response.data, response: response.response)
}

return response
Expand Down
25 changes: 25 additions & 0 deletions Sources/_Helpers/SharedModels/HTTPError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// HTTPError.swift
//
//
// Created by Guilherme Souza on 07/05/24.
//

import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// A generic error from a HTTP request.
///
/// Contains both the `Data` and `HTTPURLResponse` which you can use to extract more information about it.
public struct HTTPError: Error, Sendable {
public let data: Data
public let response: HTTPURLResponse

public init(data: Data, response: HTTPURLResponse) {
self.data = data
self.response = response
}
}

0 comments on commit 782e940

Please sign in to comment.