Git Product home page Git Product logo

actionresultexceptionhandler's Introduction

Exception Handling for ASP.NET Core

ActionResultExceptionHandler allows you to explicitly handle exception within your ASP.NET Core application, within each controller action.

Configuring your error handling this way reaps the following benefits:

  • Simple and minimalist way to handle exceptions in your Asp.Net Core applications
  • A more clean approach to try-catch logic in your controllers
  • Catch and appropriately handle specific or non specified exceptions.

This Library targets the ASP.NET Core applications.

Installation

ActionResultExceptionHandler is available on NuGet and can be installed via the below commands depending on your platform:

$ Install-Package Chibuike.Util.ActionResultExceptionHandler

or via the .NET Core CLI:

$ dotnet add package Chibuike.Util.ActionResultExceptionHandler

Bare Bones Setup in your StartUp.cs

Note: This Exception handler has to be Injected using Asp.Net Core inbuilt functionality for dependency injection.

// Startup.cs
using ActionResultExceptionHandler;

// This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<ExceptionService>();

        services.AddControllers();
    }

Handling specific exceptions in your Controller

You can explicitly handle exceptions like so:

using ActionResultExceptionHandler;

    public class ValuesController : ControllerBase
    {
        public readonly ExceptionService _exceptionService;

        public ValuesController (ExceptionService exceptionService)
        {
            _exceptionService = exceptionService;
        }


        [HttpPost("{id}")]
        public ActionResult Get(Guid id)
        {  
            try
            {
                //do something: (Get values by Id)
            }
            catch(Exception ex)
            {
                return _exceptionService.GetActionResult(ex);
            }
        }
    }

Sample Response for caught exceptions

HTTP/1.1 404 Not Found
content-type: application/json; charset=utf-8 
date: Tue, 23 Feb 2021 17:03:03 GMT 
server: Kestrel

{
  "message": "Values with id does not exist",
  "stackTrace": "   at ValueSample.Controllers.ValuesController.Get(Guid id) in C:\\Users\\Chibuike\\source\\repos\\ValueSample\\Controllers\\ValuesController.cs:line 63",
  "data": {},
  "innerException": null
}

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.