S
Supabase2w ago
Oz

Custom api response error messages

Hello! Is it possible to customize error message content for api responses? For example I want to display just 'permission denied' instead of 'permission denied for table ....'
{
"code": "42501",
"details": null,
"hint": null,
"message": "permission denied for table tenants"
}
{
"code": "42501",
"details": null,
"hint": null,
"message": "permission denied for table tenants"
}
Thank you!
12 Replies
inder
inder2w ago
you can build your own using the code in the error https://docs.postgrest.org/en/stable/references/errors.html or use this package if you're working with js lib https://github.com/srothgan/supabase-error-translator-js/tree/main haven't used this myself but looked at the source code and seems to be correct. you can port it to some other lang if you're not using js. The package handles errors from other services as well
Oz
OzOP2w ago
Thanks for the reply @inder The package you mentioned looks really useful on the client side, but I’m actually looking for a solution on the server side. I know how to create custom errors, but defining them in every function adds a bit too much overhead. Also, for queries made directly with supabase.client (like simple selects), I guess I can’t really use RAISE EXCEPTION, right? It would be great if all default error messages were centralized somewhere and we could just customize them.
inder
inder2w ago
Are you not using supabase library on server side? The error response will be the same. Do you mean RAISE EXCEPTION in pg function? If yes, That too will be returned as error
Oz
OzOP2w ago
Well, I'm accessing the supabase via supabase client (@supabase/supabase-js) and this is how I execute a function:
const { data, error } = await supabase.schema('api').rpc('fn_get_tenant')
const { data, error } = await supabase.schema('api').rpc('fn_get_tenant')
When this code runs, if the user is not logged in, the api returns a 401 Unauthorized (as expected) with a response error message "permission denied for table tenants"
POST https://my.supabase.co/rest/v1/rpc/fn_get_tenant 401 (Unathorized)
POST https://my.supabase.co/rest/v1/rpc/fn_get_tenant 401 (Unathorized)
I want to have just "permission denied" in the response error message, I don't want to expose my table name.
inder
inder2w ago
Isn't there a "code" in the error object?
Oz
OzOP2w ago
yes:
{
"code": "42501",
"details": null,
"hint": null,
"message": "permission denied for table tenants"
}
{
"code": "42501",
"details": null,
"hint": null,
"message": "permission denied for table tenants"
}
inder
inder2w ago
Ya, check for this code and send a generalized message. This is where that lib I mentioned will be handy as it checks for these codes and returns a generalized error message
inder
inder2w ago
For example
No description
inder
inder2w ago
Ofcourse you can fork and modify the messages acc. to your needs
Oz
OzOP2w ago
Thanks for your insights @inder Actually, I want to customize the response error message on the "server side". Anytime this API is called via browser, via postman, via some other client.
inder
inder2w ago
That can't be done as long as you're using hosted supabase. On self hosted, you can fork postgrest and modify the source code to customize error msg Using the error codes to customize error message is a standard practice across applications
Oz
OzOP2w ago
Maybe in the future Supabase team provides a way to customize these messages on hosted:) I really appreciate all your help @inder. Have a great day!

Did you find this page helpful?