惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
N
Netflix TechBlog - Medium
博客园 - 聂微东
Y
Y Combinator Blog
罗磊的独立博客
博客园_首页
小众软件
小众软件
有赞技术团队
有赞技术团队
爱范儿
爱范儿
F
Fortinet All Blogs
C
Check Point Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏

Netlify Developers

AI model comparison using Netlify's AI Gateway | Netlify Developers Build an AI agent to automatically process and summarize form submissions | Netlify Developers Build an AI agent to automatically generate relevant images for blog posts | Netlify Developers Prerendering SPA content pages to enable AI agent access | Netlify Developers Identify image optimization opportunities with Netlify Observability | Netlify Developers Fix top 404 page not found errors with Netlify Observability | Netlify Developers Introducing AI Gateway: built-in AI model access on Netlify | Netlify Developers Introducing Observability: your project insights on Netlify | Netlify Developers From first publish to first update with AI agents | Netlify Developers How to use Agent Runners on Netlify | Netlify Developers Add forms to your project with AI + Netlify | Netlify Developers Track Real User Metrics | Netlify Developers Lighthouse performance scores on Netlify | Netlify Developers Review your AI project before you ship | Netlify Developers Using environment variables in AI Projects on Netlify | Netlify Developers Build prototypes and MVPs fast with AI + Netlify | Netlify Developers Pause auto-publishing on Netlify | Netlify Developers Run serverless functions, storage, and more natively in Nuxt dev | Netlify Developers Netlify Office Hours: The AX Arcade challenge | Netlify Developers Netlify Office Hours: Prompting with style | Netlify Developers Netlify Office Hours: RAG Apps with OpenAI + Netlify DB | Netlify Developers How to build a RAG application with Neon, Netlify & OpenAI | Netlify Developers Netlify Office Hours: Netlify DB (powered by Neon) | Netlify Developers Netlify Office Hours: Netlify MCP Server | Netlify Developers Netlify Office Hours: Netlify Vite Plugin | Netlify Developers Building MCPs with Netlify | Netlify Developers Deploying sites from your AI tool | Netlify Developers Adding your domain using Netlify API | Netlify Developers Build a context driven chat bot with Netlify Blob and OpenAI | Netlify Developers Simplify deployments with Netlify’s branch-matching environment variables | Netlify Developers
Using custom error handling if an edge function has crash...
2024-03-22 · via Netlify Developers

When code is executed at request-time rather than at build time, and if it influences what visitors to your site see, then it is vitally important that you can control what happens should that code encounter an error. Displaying a message like “an edge function has crashed” or a cryptic HTTP error code makes for a rough user experience. That’s why Netlify Edge Functions give you some helpful options to gracefully handle errors, and fully control the experience passed on to the users.

#TL;DR

We’ll look at how to configure the custom error handling available in Netlify Edge Functions, and try them out with some simplified examples which you can also clone and explore in your own local and deployed sites.

#The default error page

If an error ever causes an edge function to crash, that error is written to the edge function logs and by default it will also be displayed as a response from the Edge Function. This default edge function error page is helpful for the developer, but not so much for the user.

The default edge function error page

#Custom error handling in edge functions

You can configure this error handling behavior to customize the experience. You have three options:

  1. Show the default failure page
  2. Silently bypass the error
  3. Display your own custom error page

#1. Show the default failure page

The custom error handling is configured directly in your edge function by adding an onError property to the config. If you don’t specify an onError behavior, the default of fail will be used, as you can see here.

// /netlify/edge-functions/any-function.ts

import { Config } from "@netlify/edge-functions";

export default async () => {

throw new Error("error");

};

export const config: Config = {

// Display the failure message

// (This is the default behavior if not configured)

onError: "fail",

};

#2. Silently bypass the error

By setting the onError option to bypass, an erroring edge function still throws an error which is entered into the error logs, but it does not halt the progress of the request chain. If an asset was to be returned from this request, that will still happen even though the function encountered an error.

You can see that in this example, where an edge function is invoked on requests made to the intended page. Even though the function errors, you’ll still reach the intended page.

// /netlify/edge-functions/any-function.ts

import { Config } from "@netlify/edge-functions";

export default async () => {

throw new Error("error");

};

export const config: Config = {

// Silently bypass the error

onError: "bypass",

};

#3. Display your own custom error page

By setting the onError option to hold any path on your site, starting with /, the Edge function still throws an error which is entered into the error logs, but the user sees the result of an HTTP rewrite to the path of our choosing within the site. This allows us to let them down gently with whatever nicely designed experience we chose, while logging the error behind the scenes.

You can see that in this example where an edge function is invoked on requests made to the this erroring route which results in you seeing the custom error page.

// /netlify/edge-functions/any-function.ts

import { Config } from "@netlify/edge-functions";

export default async () => {

throw new Error("error");

};

export const config: Config = {

// Redirect the user to any URL in your site

onError: "/your-far-prettier-error-page",

};

#Try for yourself

Visiting these example links only showed you the view intended for the user. To see the error logging behind the scenes, and to experiment more, you can clone and deploy your own version of these simple examples by clicking the button below.

Deploy to Netlify

#Further reading