Skip to content

Allow SupabaseClient to override Postgres error handling #101

Closed
@kurtguenther

Description

@kurtguenther

Problem: Unless I'm doing something wrong, I think it's not possible for a consumer to override the default implementation of the APIClientDelegate validateResponse function on the SupabaseClient, which will cause this code to always be executed:

    func client(_ client: APIClient, validateResponse response: HTTPURLResponse, data: Data, task: URLSessionTask) throws {
        guard (200..<300).contains(response.statusCode) else {
            throw APIError.unacceptableStatusCode(response.statusCode)
        }
    }

** This hides all the good Postgres details that are helpful for debugging. **

Describe the solution you'd like

I tried to override SupabaseClient outside of this module inside my own module, but it seems like the default implementation is always overriding (again unless I'm doing something wrong).

I ended up having to fork to include this (though a proper way may be to have SupabaseClient.init(...) allow for its own delegate an follow the MultiAPIClientDelegate compose pattern that the PostgresClient is using.

extension SupabaseClient: APIClientDelegate {
  public func client(_: APIClient, willSendRequest request: inout URLRequest) async throws {
    request = await adapt(request: request)
  }

  public func client(_ client: APIClient, validateResponse response: HTTPURLResponse, data: Data, task: URLSessionTask) throws {
    if 200 ..< 300 ~= response.statusCode {
      return
    }

    throw try client.configuration.decoder.decode(PostgrestError.self, from: data)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpostgrestWork related to postgrest package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions