mirror of
https://github.com/supabase/supabase.git
synced 2026-05-16 15:49:19 +08:00
* Update Environment Variables docs * Update Dependencies docs * Add Function Configuration d ocs * Add custom Configuration section in Navigation * Add Error Handling section * Add new HTTP Methods section * Update Deno 2 Guide * Update nav * Rm deno2 nav * Update Deploy section * Update CI/CD section * Update Logging * Update Testing * Update Local Debugging * Update Troubleshooting * Update Nav * Update CLI Quickstart * Update Dashboard Quickstart * Update Development Environment * Update Nav * Fix links * Update apps/docs/content/guides/functions/quickstart-dashboard.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/development-environment.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/development-environment.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/quickstart.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Use $CodeSample for multi-root example * Use tocVideo instead of video embed * Rm typo * Switch Quickstart ordering * Rephrase Docker requirement for deployment * Add Project Structure section * style: fix docs lint issues * Update Regional Invocations * Update Error Handing * Fix linting * Fixes * Remove duplicate sections * Update Auth integration * Update Storage * Update Database/Postgres * Update Nav * Update Background Tasks * Update Ephemeral Storage * Update Websockets * Update Routing * Update wasm * Update AI models * Fix linting issues * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/storage-caching.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/websockets.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/websockets.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Update apps/docs/content/guides/functions/ai-models.mdx Co-authored-by: Charis <26616127+charislam@users.noreply.github.com> * Rm unnecessary lines * Rm wrong example * Fix typo * Rm deno2 * Move CICD to deployment and rename routing * Fix whitespace * rm unnecessary spelling --------- Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
78 lines
2.7 KiB
Plaintext
78 lines
2.7 KiB
Plaintext
---
|
|
id: error-handling
|
|
title: Error Handling
|
|
description: Learn how to handle errors in your Edge Functions.
|
|
subtitle: 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:
|
|
|
|
```tsx
|
|
Deno.serve(async (req) => {
|
|
try {
|
|
// Your function logic here
|
|
const result = await processRequest(req)
|
|
return new Response(JSON.stringify(result), {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
status: 200,
|
|
})
|
|
} catch (error) {
|
|
console.error('Function error:', error)
|
|
return new Response(JSON.stringify({ error: error.message }), {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
status: 500,
|
|
})
|
|
}
|
|
})
|
|
```
|
|
|
|
**Best practices for function errors:**
|
|
|
|
- Use the right HTTP status code for each situation. Return `400` for bad user input, 404 when something doesn't exist, 500 for server errors, etc. This helps with debugging and lets client apps handle different error types appropriately.
|
|
- Include helpful error messages in the response body
|
|
- Log errors to the console for debugging (visible in the Logs tab)
|
|
|
|
---
|
|
|
|
## Client-side error handling
|
|
|
|
Within your client-side code, an Edge Function can throw three types of errors:
|
|
|
|
- **`FunctionsHttpError`**: Your function executed but returned an error (4xx/5xx status)
|
|
- **`FunctionsRelayError`**: Network issue between client and Supabase
|
|
- **`FunctionsFetchError`**: Function couldn't be reached at all
|
|
|
|
```jsx
|
|
import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from '@supabase/supabase-js'
|
|
|
|
const { data, error } = await supabase.functions.invoke('hello', {
|
|
headers: { 'my-custom-header': 'my-custom-header-value' },
|
|
body: { foo: 'bar' },
|
|
})
|
|
|
|
if (error instanceof FunctionsHttpError) {
|
|
const errorMessage = await error.context.json()
|
|
console.log('Function returned an error', errorMessage)
|
|
} else if (error instanceof FunctionsRelayError) {
|
|
console.log('Relay error:', error.message)
|
|
} else if (error instanceof FunctionsFetchError) {
|
|
console.log('Fetch error:', error.message)
|
|
}
|
|
```
|
|
|
|
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.
|
|
|
|

|
|
|
|
For more information on Logging, check out [this guide](/docs/guides/functions/logging).
|