supabase icon indicating copy to clipboard operation
supabase copied to clipboard

[v2] Enable error handling for `getPublicUrl` (via `error` return or allowing to throw)

Open bennettdams opened this issue 3 years ago • 2 comments

Feature request

Is your feature request related to a problem? Please describe.

Before Supabase v2, the storage's getPublicUrl returned an error, something like this:

  const { publicURL, error } = supabase.storage
    .from(STORAGE.AVATARS.bucket)
    .getPublicUrl(...)

This allowed the caller (or calling this in an abstracted function) to do error handling. This was unfortunately removed in v2, so error handling cannot work anymore.

Example: In my case, I want to show a fallback avatar if getting the public URL was not successful.

Describe the solution you'd like

I think there are two options:

  1. Provide an error field like before:

    const { publicURL, error } = supabase.storage....
    
  2. Allow getPublicUrl to throw via config, something like:

    .getPublicUrl(..., {
      throwOnError: true,
    })
    

    This second options parameter already exists, so this can be introduced as an additional field. One could now try/catch himself to do the appropriate error handling.

bennettdams avatar Nov 09 '22 00:11 bennettdams

I don't think getPublicUrl ever actually returned anything but null for error. It does not make a storage api call, and just puts together a string with the pathname, bucket and your instance url with some constants.

GaryAustin1 avatar Nov 09 '22 01:11 GaryAustin1

It does not make a storage api call

Ah okay, but you can see the 400 request for a non-existing file nevertheless in the devtools.

Maybe my feature request should be rephrased to storage.from instead of getPublicUrl?

Or, in other words: How do you know you tried to access file that does not exist?

bennettdams avatar Nov 09 '22 01:11 bennettdams

I'm going to close this out. getPublicURL can't do any error checking and is just a helper function that concatenates strings. It does not leave your client at all. It will never be able to know if your file exists or not.

Only when you use it to actually fetch the file would an error be possible.

GaryAustin1 avatar Feb 11 '23 23:02 GaryAustin1