Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

WebApi global ExceptionHandler always returns HTML instead of JSON when package SerilogWeb.Classic.WebApi is present in project #16

Closed
victorvogelpoel opened this issue Feb 27, 2019 · 8 comments

Comments

@victorvogelpoel
Copy link

I have an global ExceptionHandler in my WebAPI. Any response from the ExceptionHandler is returned as HTML and not JSON, which is the default content encoding.

Upon removing the SerilogWeb.Classic.WebApi package from the project, responses are no longer in HTML, but now in JSON.

Stackoverflow https://stackoverflow.com/questions/47778018/exceptionhandler-being-called-but-not-returning-json describes the issue and also the culprit, which is SerilogWeb.Classic.WebApi.

@tsimbalar
Copy link
Member

tsimbalar commented Feb 27, 2019

Hi Victor,

thanks for sharing.

It looks very similar to #3 that said :

When I'm using own ExceptionFilterAttribute and SerilogWeb.Classic.WebApi I get always back status code 500 and HTML as response when exception is thrown, ignoring the Accept header and defined status code as to be returned

Would love to be able to fix it.

Do you happen to have a minimal repro project that we could use to analyze and try to fix the issue ?

We also had a discussion going on there in SerilogWeb.Classic to improve the mechanism we use to report exceptions, if you can chime in ;)

Thanks

@victorvogelpoel
Copy link
Author

It reads as something similar, yes.
And no sorry, I don't have a minimal project, just a client's project.

Have to whip up something...

@victorvogelpoel
Copy link
Author

Minimal project added. The ZIP contains no packages due to issue file upload constraints; you'll have to do a package restore.

SerilogWeb.Classic.WebApi is referenced in this project. Requesting /values/5 will throw a IDNotFoundException, which is intercepted by the global exception handler. The handler returns an HTML page when SerilogWeb.Classic.WebApi is referenced.

Remove SerilogWeb.Classic.WebApi package from the project and comment out its methods in the code and web.config. Now request /values/5 again, and it will show the exception message in JSON.

Good hunting!

WebAPISerilogClassicIssue2.zip

@tsimbalar
Copy link
Member

Thanks Victor, that's super useful !

I guess that's the push we needed to start implementing serilog-web/classic#48 and have a less intrusive integration ;)

Will keep you posted !

tsimbalar added a commit to serilog-web/classic that referenced this issue Feb 28, 2019
Allow to report errors to the module through a less intrusive mechanism

- Introduces new extension methods under namespace `SerilogWeb.Classic.Extensions`, mainly to be used by `SerilogWeb.CLassic.WebAPI` for serilog-web/classic-webapi#16 :
  - `HttpContext.AddSerilogWebError(Exception exception)` / `HttpContextBase.AddSerilogWebError(Exception exception)`
  - `HttpContext.GetLastSerilogWebError()` / `HttpContextBase.GetLastSerilogWebError()` 
- bump referenced Serilog version to **v2.7.1** (published 9 month ago) . (v2.8.0 is only a month old, and forcing unnecessary update cold be annoying to end user)
- bump package version to **v5.0** . Not really a breaking change but may introduce subtle behavior different when combined with secondary packages (`WebAPI` one)
tsimbalar added a commit to tsimbalar/classic-webapi that referenced this issue Feb 28, 2019
…to SerilogWeb module

Instead rely on ad-hoc extension method that stores it temporarily in HttpContext

Related to serilog-web#16 and serilog-web#3
@tsimbalar
Copy link
Member

tsimbalar commented Feb 28, 2019

This should now be fixed as per SerilogWeb.Classic.WebApi v4.0.5 . It does at least fix it in your repro 🎉 !

I would love it if you could give it a try and confirm that it solves your issue !

@victorvogelpoel
Copy link
Author

Thanks @tsimbalar. I'll check tomorrow morning.

@victorvogelpoel
Copy link
Author

YES! issue is solved! Thanks a lot!

@tsimbalar
Copy link
Member

tsimbalar commented Mar 1, 2019 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants