GithubHelp home page GithubHelp logo

nosratifarhad / global_error_handling_dotnet_8 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 15 KB

Several methods Global Error Handling use ProblemDetails With DotNet 8

C# 100.00%
dotnet dotnet-core exception-handling exceptions middleware problemdetails

global_error_handling_dotnet_8's Introduction

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 ...

global_error_handling_dotnet_8's People

Contributors

nosratifarhad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.