abergs / OwinFriendlyExceptions

Owin Middleware to catch exceptions and translate them into nice HTTP responses
MIT License
23 stars 7 forks source link

No Matter what I do always return HTML stack trace #11

Closed jmichaelbrown closed 6 years ago

jmichaelbrown commented 7 years ago

Seems to be functioning fine, TransformException is adding correct contenttype, statuscode etc but client always receives stack trace with none of the desired data and no json.

What am I doing wrong?

abergs commented 7 years ago

Make sure you specify your contentGenerator Func<TEx, string> argument in .To.

Ex:

.Map<Exception>
.To(HttpStatusCode.InternalServerError, "Operation Canceled", ex => ex.Message)

vs

.Map<Exception>
.To(HttpStatusCode.InternalServerError, "Operation Canceled", ex => JsonConvert.Serialize(ex), "application/json")

.To will just write whatever you return from the contentGenerator, so you have total control over errorFormat

abergs commented 7 years ago

@jmichaelbrown Could you also put a breakpoint in the contentGenerator to make sure you hit it? Because depending on what you are running down-stream (WebApi, WebApi2, Nancy) they often try to swallow the exceptions and write them out to the HTTP stream themself. You will need to use our adapters to hook in, there is currently one for WebApi2. Check the readme for installation guidelines.

jmichaelbrown commented 7 years ago

Sorry for the delay, I am just now circling back around to this. By adapter I am assuming you mean app.UseFriendlyExceptions(this.GetFriendlyExceptions(), new[] { new WebApi2ExceptionProvider() }); config.Services.Replace(typeof(IExceptionHandler), new WebApi2ExceptionHandler(this.GetFriendlyExceptions()));

I had already done this, however It's still not working, I would like to figure out what's going on here in case anyone else runs into the same issue. I have located what I believe is your "contentGenerator" Is there any particular area I need to check for... like public ITransformsMap? Because it is hitting this

abergs commented 7 years ago

Did you make sure you apply before the rest of your middlewares?

// Use FriendlyExceptions before your other middlewares
                app.UseFriendlyExceptions(GetFriendlyExceptions(), new [] {new WebApi2ExceptionProvider()});
// mvc and stuff