question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

map my custom exception to ExceptionProblemDetails

See original GitHub issue

Hi dears, at first, I read your sample in the GitHub, run it, and fine worked.

but in my real application, I have a Custom exception class that used in the domain layer. this class derived from System.Exception. and has multiple extra property(errorCode, errorMsg, StatusCode).

now I want to map my exception class to ExceptionProblemDetails class when configure the problem detail in ConfigureServices() in the startup.

I know also you map with this code:

options.Map<MyCustomException>(ex => new ExceptionProblemDetails(ex, (int)ex.HttpStatusCode));

can I map my properties to the property of the problem detail class before write response by the ProblemDetailsMiddleware. for example(mapping):

new ProblemDetails() { Title = myCustomException.Code.ToString(), Detail = myCustomException.Message, Status = (int?)myCustomException.HttpStatusCode })

thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
khellangcommented, Feb 4, 2019

but didn’t log throwed exceptions of type ‘MyCustomException’.

Right. That’s because the status code is 400, and we only log 500+ error codes by default. I pushed a new version to NuGet that allows you to set

https://github.com/khellang/Middleware/blob/10d508e1071a86dd59360b0f15760aca0e7d43ba/src/ProblemDetails/ProblemDetailsOptions.cs#L43

when configuring the middleware. It will allow you to tweak when the middleware will log an exception as “unhandled”, as people seem to want different things here.

Example:

services.AddProblemDetails(options =>
{
    // The following predicate get both the exception and the mapped ProblemDetails instance
    // passed in. This will give you the ultimate flexibility to decide whether you want to
    // log the exception as "unhandled" or not :)
    options.ShouldLogUnhandledException = (e, d) =>
    {
       	return e is MyCustomException || (d.Status.HasValue && d.Status.Value >= 500);
    }
});
0reactions
khellangcommented, Feb 4, 2019

Great! Let me know if it works so we can close this issue 👌

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a clean way to map specific exception types ...
I tried as below,is it what you want? [HttpPost] public IActionResult Post(string Path) { throw new FileNotFoundException(); }.
Read more >
Handling Web API Exceptions with ProblemDetails ...
In this short post I describe a handy error-handling middleware, created by Kristian Hellang, that is used to return ProblemDetails results ...
Read more >
Using the ProblemDetails Class in ASP.NET Core Web API
If we want to create our custom exception class and map it to the problem details object created by the middleware, we can...
Read more >
Returning exceptions as ASP.NET Core Problem Details
NET to generate exception information. These classes can be used by themselves or as base classes for your own HttpExceptions.
Read more >
Handle errors in ASP.NET Core web APIs
An unhandled exception occurs. The automatic creation of a ProblemDetails for error status codes is disabled when the SuppressMapClientErrors ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found