C
C#2y ago
HAHOOS

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:
Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.
Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.
I don't know what to do, I looked everywhere on the internet and nothing, here is my code for executing an error:
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(JsonConvert.SerializeObject("Code has been not found"), Encoding.UTF8, "application/json"),
ReasonPhrase = "Code Not Found",
};
throw new HttpResponseException(resp);
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(JsonConvert.SerializeObject("Code has been not found"), Encoding.UTF8, "application/json"),
ReasonPhrase = "Code Not Found",
};
throw new HttpResponseException(resp);
8 Replies
Joao
Joao2y ago
@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.
HAHOOS
HAHOOS2y ago
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 database
FusedQyou
FusedQyou2y ago
You 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.
Joao
Joao2y ago
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-handling
Exception 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.
FusedQyou
FusedQyou2y ago
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 🤷
HAHOOS
HAHOOS2y ago
I got it working, but I want it shorter so it will only display message and code. My current response:
{
"version": "1.1",
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"application/json; charset=utf-8"
]
}
]
},
"statusCode": 404,
"reasonPhrase": "Code Not Found",
"headers": [],
"trailingHeaders": [],
"requestMessage": null,
"isSuccessStatusCode": false
}
{
"version": "1.1",
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"application/json; charset=utf-8"
]
}
]
},
"statusCode": 404,
"reasonPhrase": "Code Not Found",
"headers": [],
"trailingHeaders": [],
"requestMessage": null,
"isSuccessStatusCode": false
}
It now outputs HttpResponseMessage and the code is:
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(JsonConvert.SerializeObject("Code has been not found"), Encoding.UTF8, "application/json"),
ReasonPhrase = "Code Not Found",
};
return resp;
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(JsonConvert.SerializeObject("Code has been not found"), Encoding.UTF8, "application/json"),
ReasonPhrase = "Code Not Found",
};
return resp;
FusedQyou
FusedQyou2y ago
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.
Accord
Accord2y ago
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.