rembrembdocs

Error Handling

Implement proper error responses and client-side handling to create reliable applications.


Error handling#

Implementing the right error responses and client-side handling helps with debugging and makes your functions much easier to maintain in production.

Within your Edge Functions, return proper HTTP status codes and error messages:

1Deno.serve(async (req) => {2  try {3    // Your function logic here4    const result = await processRequest(req)5    return new Response(JSON.stringify(result), {6      headers: { 'Content-Type': 'application/json' },7      status: 200,8    })9  } catch (error) {10    console.error('Function error:', error)11    return new Response(JSON.stringify({ error: error.message }), {12      headers: { 'Content-Type': 'application/json' },13      status: 500,14    })15  }16})

Best practices for function errors:


Client-side error handling#

Within your client-side code, an Edge Function can throw three types of errors:

1import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from '@supabase/supabase-js'23const { data, error } = await supabase.functions.invoke('hello', {4  headers: { 'my-custom-header': 'my-custom-header-value' },5  body: { foo: 'bar' },6})78if (error instanceof FunctionsHttpError) {9  const errorMessage = await error.context.json()10  console.log('Function returned an error', errorMessage)11} else if (error instanceof FunctionsRelayError) {12  console.log('Relay error:', error.message)13} else if (error instanceof FunctionsFetchError) {14  console.log('Fetch error:', error.message)15}

Make sure to handle the errors properly. Functions that fail silently are hard to debug, functions with clear error messages get fixed fast.


Error monitoring#

You can see the production error logs in the Logs tab of your Supabase Dashboard.

Function invocations.

For more information on Logging, check out this guide.