Giter Site home page Giter Site logo

Comments (4)

josephwoodward avatar josephwoodward commented on June 1, 2024

Yes, if I'm understanding you correctly then this is the expected behaviour. If an exception isn't mapped then the response GlobalExceptionHandler will return to callers is the default response.

Take the following example:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseGlobalExceptionHandler(x => {
    x.ContentType = "application/json";
   // This is a default error response
    x.ResponseBody(s => JsonConvert.SerializeObject(new
    {
        Message = "An error occurred whilst processing your request"
    }));

    x.Map<ValidationException>().ToStatusCode(StatusCodes.Status400BadRequest);
});
    
     // Returns 400 plus whatever is in default response ResponseBody (eg: "An error occurred whilst processing your request")
    app.Map("/validationerror", x => x.Run(y => throw new ValidationException()));

   // returns whatever is configured in the default response ResponseBody (eg: "An error occurred whilst processing your request")
    app.Map("/applicationerror", x => x.Run(y => throw new ApplicationException()));
}

I hope this helps, let me know if you have any further problems or questions.

from globalexceptionhandlerdotnet.

Kromi75 avatar Kromi75 commented on June 1, 2024

I'm not sure if we talk about the same thing. Please have a look at this snippet:

      app.UseGlobalExceptionHandler(
        x =>
        {
          x.ContentType = "application/json";
          x.ResponseBody(s => JsonConvert.SerializeObject(new { Message = "An error occurred whilst processing your request" }));

          x.OnError((exception, httpContext) =>
          {
            // This is not executed for exceptions of type ApplicationException.
            Log.Error("An error occurred. {Exception}", exception);
            return Task.CompletedTask;
          });

          // Validation-Exceptions are to be returned as Bad Request.
          x.Map<ValidationException>().ToStatusCode(StatusCodes.Status400BadRequest);
        },
        NullLoggerFactory.Instance);

With this configuration, the OnError func is called for exceptions of type ValidationException, but not for ApplicationException. So I assume that the OnError func is only called for exception types a mapping is defined for. Am I right? Or am I doing it completely wrong?
BTW, what I'm trying to achieve is that all exceptions are logged exactly once. That's why I passed a NullLoggerFactory to avoid having all exceptions logged as unhandled exceptions.

from globalexceptionhandlerdotnet.

josephwoodward avatar josephwoodward commented on June 1, 2024

This should be fixed in the v5 release which will be going out in the next day or so.

from globalexceptionhandlerdotnet.

Kromi75 avatar Kromi75 commented on June 1, 2024

Great, thank you.

from globalexceptionhandlerdotnet.

Related Issues (20)

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.