supabase-community / supabase-kt

A Kotlin Multiplatform Client for Supabase.
https://supabase.com/docs/reference/kotlin/introduction
MIT License
415 stars 37 forks source link

[Feature request]: Include response in RestException #774

Open DominicGBauer opened 2 weeks ago

DominicGBauer commented 2 weeks ago

General Info

Feature request

Please can the response be included in the RestException as I need access to the code that is in the response?

(Similar change happened here https://github.com/supabase-community/supabase-kt/commit/d3ee40fd5c81a7cfd02fc12f4b6c5da7ccaabd04)

Usecase

I currently need to use this interceptor to get access to the code from the response to be used in my error catch:

        // This retrieves the error code from the response
        // as this is not accessible in the Supabase client RestException
        // to handle fatal Postgres errors
        supabaseClient.httpClient.httpClient.plugin(HttpSend).intercept { request ->
            val resp = execute(request)
            val response = resp.response
            if (response.status.value == 400) {
                val responseText = response.bodyAsText()

                try {
                    val error = Json { coerceInputValues = true }.decodeFromString<Map<String, String?>>(responseText)
                    errorCode = error["code"]
                } catch (e: Exception) {
                    Logger.e("Failed to parse error response: $e")
                }
            }
            resp
        }

Ideally, I could just do something like this


catch(e: Exception) {
     when (e) {
          is BadRequestRestException -> {
            (if e.code = some code) {
                 // Handle error
            }
      }
}
jan-tennert commented 2 weeks ago

Which module has this code error field? Postgrest?

DominicGBauer commented 2 weeks ago

Yeah

https://github.com/PostgREST/postgrest/blob/da0f48ea9254f278686e07d5d658cff11502e5fe/docs/references/errors.rst#L98

E.g. https://github.com/PostgREST/postgrest/blob/da0f48ea9254f278686e07d5d658cff11502e5fe/test/spec/Feature/Auth/AuthSpec.hs#L22

jan-tennert commented 2 weeks ago

Alright, I think the best way is to add a custom exception for Postgrest (like the AuthRestException) exposing these properties. But nonetheless, we can also expose the whole response in the RestExceptions in general. I'll take a look at it and see if we can make it into 3.1.0.