supabase / functions-js

MIT License
62 stars 22 forks source link

Non-200 status Responses do not expose message #55

Open cryptoseneca opened 1 year ago

cryptoseneca commented 1 year ago

Bug report

Describe the bug

When returning responses from Edge Functions, Documentation suggests new Response('..', {status: 200}). This works fine as one can see the success message in the data var. When returning a Response with a non-200 status code, the message is not available to the client.

edge func:

    return new Response(
      JSON.stringify({ success: `all went through smoothly!` }),
      {
        status: 200, // 200 or 400
      },
    );

client:

    const { data, error } = await supabase.from('table').select('*').eq('id', id);

When it is a 200 response, data will display the message but when it is non-200 (eg 400), the error var does not show the message - it only returns a generic message:

FunctionsHttpError: Edge Function returned a non-2xx status code

To Reproduce

Explained above.

Expected behavior

error to contain whatever message is added to the Response object.

System information

bombillazo commented 1 year ago

This simple, if the response .ok value is false (only true for values 200-299), the code throws a FunctionsHTTPError:

https://github.com/supabase/functions-js/blob/af4113a3368f812f491dfac0c1482460698dfdd1/src/FunctionsClient.ts#L97-L99

This error is gobbling up the error thrown and returns a generic message:

https://github.com/supabase/functions-js/blob/af4113a3368f812f491dfac0c1482460698dfdd1/src/types.ts#L38-L42

These custom function error classes are opinionated and override the expected error response of the edge function logic. However, the error returned has a context property that contains what you would expect to receive.

https://github.com/supabase/functions-js/blob/af4113a3368f812f491dfac0c1482460698dfdd1/src/FunctionsClient.ts#L80-L99

skhetcho commented 7 months ago

+1

lavisht22 commented 6 months ago

+1

bettysteger commented 4 months ago

If anyone comes here and just want to show the error, this was my solution: https://supabase.com/docs/guides/functions/quickstart#error-handling

import { FunctionsHttpError } from '@supabase/supabase-js'

const { data, error } = await supabase.functions.invoke('...')

if (error && error instanceof FunctionsHttpError) {
  const errorMessage = await error.context.json()
  console.log('Function returned an error', errorMessage)
}