Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by...
Hello, I am working on an API in MVC and I want throw an exception, but I get the following error:
I don't know what to do, I looked everywhere on the internet and nothing, here is my code for executing an error:
8 Replies
@HAHOOS your application is probably not handling the exception thrown in the controller. Is there a specific benefit to throw an exception in the example given?
You could, as alternative return an HttpNotFoundResult:
return new HttpNotFoundResult("optional description");
Or in .NET latest versions return ProblemDetails
or a NotFound
objects.Sadly, I can't return a result, because as an output I got a custom object called
Pending
A benefit for throwing an exception in the example is to notify the user/code that there was no code found in the databaseYou don't throw exceptions for that
Exceptions are for errors, not stuff that can happen when using the program like it's supposed to be used
Not finding a resource is not an error
I assume your code is now returning a NotFound embedded inside an InternalServerError, which is not how it's supposed to be
If you properly implement "negative" responses, their code all start with a 4 or 5 (NotFound is 404, InternalServerError is 500). 5 usually means an error, and 4 means a negative response that can happen when using the application like normal.
In my humble opinion the application should not return the object
Pending
because that resource does not exist in the case of a 404 HTTP response code. This is something you could consider while designing your application.
If you want to stick to the exception you need to make sure your application handles it, see this example: https://learn.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handlingException Handling in ASP.NET Web API - ASP.NET 4.x
Describes ASP.NET Web API executes error and exception handling and provides examples for errors and exceptions.
Pretty sure if you properly return a response like a 404, you can find some
isPositiveResponse
parameter you can check. Then just check if the code is a 404, or return an error otherwise.
Or maybe not, this is MVC after all, and I would not be surprised if their whole structure is different 🤷I got it working, but I want it shorter so it will only display
message
and code
. My current response:
It now outputs HttpResponseMessage
and the code is:
Well you can adjust the response using a middleware, but I can tell you out of experience that modifying a stream to output a custom response is very shit to work with. It works, but it's going to require A LOT of code
That said, this response if very informative. I understand it's of no use to you, but this is how a proper response should look like, and the only benefit of making it shorter is only because you don't need it currently.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.