nosratifarhad / Global_Error_Handling_ProblemDetail_DotNet_8

Several methods Global Error Handling use ProblemDetails With DotNet 8

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Global Error Handling

Note: Currently, I have implemented two types in this repository, and in the future, other methods will be added.

You can use this methods for handling errors globally.

> First mrthod use middleware:

public async Task InvokeAsync(HttpContext context)
{
    try
    {
        await _next(context);
    }
    catch (Exception exception)
    {
        var problemDetails = new ProblemDetails
        {
            Status = GetStatusCode(exception),
            Title = GetTitle(exception),
            Type = "Server Error",
            Detail = exception.Message
        };

        context.Response.StatusCode =
            StatusCodes.Status500InternalServerError;

        await context.Response.WriteAsJsonAsync(problemDetails);
    }
}

now must add middleware in program.cs file .

app.UseMiddleware<ExceptionHandlingMiddleware>();

In these conditions, any exception thrown in the code will be managed by this middleware wherever it occurs.

> the second mrthod use middleware:

You must be use dotnet 8 for can use Interface "IExceptionHandler" in project.

internal sealed class GlobalExceptionHandler : IExceptionHandler

You must be implement method TryHandleAsync.

    public async ValueTask<bool> TryHandleAsync(
        HttpContext httpContext,
        Exception exception,
        CancellationToken cancellationToken)
    {
        _logger.LogError(
            exception, "Exception occurred: {Message}", exception.Message);

        var problemDetails = new ProblemDetails
        {
            Status = GetStatusCode(exception),
            Title = GetTitle(exception),
            Type = "Server Error",
            Detail = exception.Message
        };

        httpContext.Response.StatusCode = problemDetails.Status.Value;

        await httpContext.Response
            .WriteAsJsonAsync(problemDetails, cancellationToken);

        return true;
    }

now must register GlobalExceptionHandler class and ProblemDetails in program.cs file like below :

builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();

and add middleware :

app.UseExceptionHandler();

Writing better documentary ...

About

Several methods Global Error Handling use ProblemDetails With DotNet 8


Languages

Language:C# 100.0%