From 57155d65ec07e04aa2c20c3831f00081eb823aa3 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 8 Sep 2022 16:52:40 -1000 Subject: [PATCH 01/38] moniker prep --- aspnetcore/fundamentals/error-handling.md | 4 +- aspnetcore/web-api/handle-errors.md | 174 +++++++++++++++++++++- 2 files changed, 174 insertions(+), 4 deletions(-) diff --git a/aspnetcore/fundamentals/error-handling.md b/aspnetcore/fundamentals/error-handling.md index 63af9083af07..cce08bb75581 100644 --- a/aspnetcore/fundamentals/error-handling.md +++ b/aspnetcore/fundamentals/error-handling.md @@ -5,14 +5,14 @@ description: Discover how to handle errors in ASP.NET Core apps. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 11/09/2021 +ms.date: 9/9/2021 uid: fundamentals/error-handling --- # Handle errors in ASP.NET Core :::moniker range=">= aspnetcore-6.0" -By [Kirk Larkin](https://twitter.com/serpent5), [Tom Dykstra](https://github.com/tdykstra/), and [Steve Smith](https://ardalis.com/) +By [Tom Dykstra](https://github.com/tdykstra/) This article covers common approaches to handling errors in ASP.NET Core web apps. See for web APIs. diff --git a/aspnetcore/web-api/handle-errors.md b/aspnetcore/web-api/handle-errors.md index 117090d47651..576b2b185af8 100644 --- a/aspnetcore/web-api/handle-errors.md +++ b/aspnetcore/web-api/handle-errors.md @@ -5,12 +5,182 @@ description: Learn about error handling with ASP.NET Core web APIs. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 11/09/2021 +ms.date: 9/09/2022 uid: web-api/handle-errors --- # Handle errors in ASP.NET Core web APIs -:::moniker range=">= aspnetcore-6.0" +:::moniker range=">= aspnetcore-7.0" + +This article describes how to handle errors and customize error handling with ASP.NET Core web APIs. + +## Developer Exception Page + +The [Developer Exception Page](xref:fundamentals/error-handling) shows detailed stack traces for server errors. It uses to capture synchronous and asynchronous exceptions from the HTTP pipeline and to generate error responses. For example, consider the following controller action, which throws an exception: + +:::code language="csharp" source="handle-errors/samples/6.x/HandleErrorsSample/Controllers/ErrorsController.cs" id="snippet_Throw"::: + +When the Developer Exception Page detects an unhandled exception, it generates a default plain-text response similar to the following example: + +```console +HTTP/1.1 500 Internal Server Error +Content-Type: text/plain; charset=utf-8 +Server: Kestrel +Transfer-Encoding: chunked + +System.Exception: Sample exception. + at HandleErrorsSample.Controllers.ErrorsController.Get() in ... + at lambda_method1(Closure , Object , Object[] ) + at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) + at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() + at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) + at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() + +... +``` + +If the client requests an HTML-formatted response, the Developer Exception Page generates a response similar to the following example: + +```console +HTTP/1.1 500 Internal Server Error +Content-Type: text/html; charset=utf-8 +Server: Kestrel +Transfer-Encoding: chunked + + + + + + Internal Server Error +