functions-js icon indicating copy to clipboard operation
functions-js copied to clipboard

Non-200 status Responses do not expose message

Open cryptoseneca opened this issue 2 years ago • 6 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

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

  • OS: macOS
  • Version of supabase-js: "@supabase/supabase-js": "^2.20.0"
  • Version of Node.js: v16.17.1

cryptoseneca avatar Apr 20 '23 15:04 cryptoseneca

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

bombillazo avatar Jun 09 '23 03:06 bombillazo

+1

skhetcho avatar Feb 13 '24 08:02 skhetcho

+1

lavisht22 avatar Feb 23 '24 12:02 lavisht22

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)
}

bettysteger avatar Apr 21 '24 20:04 bettysteger