Skip to content

fix: throw generic HTTPError #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}