C
C#β€’5mo ago
Tarkacode

βœ… My API returns 500 internal server error with the SQL exception text, why?

Hello every one, I built an ASP.NET API in .NET and I wrote a controller Action that looks like this :
[HttpPost]
public async Task<ActionResult<RecipeIngredientDto>> Post(RecipeIngredientRequest recipeIngredientRequest)
{
try
{
RecipeIngredient recipeIngredient = recipeIngredientRequest.MapToRecipeIngredient();

await _recipeIngredientRepository.Create(recipeIngredient);

RecipeIngredientDto dto = recipeIngredient.MapToDto();
return Ok(dto);
// TODO: Return CreatedAtAction when Get action is made
}
catch (ArgumentOutOfRangeException ex)
{
return BadRequest(ex.Message);
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
[HttpPost]
public async Task<ActionResult<RecipeIngredientDto>> Post(RecipeIngredientRequest recipeIngredientRequest)
{
try
{
RecipeIngredient recipeIngredient = recipeIngredientRequest.MapToRecipeIngredient();

await _recipeIngredientRepository.Create(recipeIngredient);

RecipeIngredientDto dto = recipeIngredient.MapToDto();
return Ok(dto);
// TODO: Return CreatedAtAction when Get action is made
}
catch (ArgumentOutOfRangeException ex)
{
return BadRequest(ex.Message);
}
catch (ArgumentException ex)
{
return BadRequest(ex.Message);
}
}
So, as you can see, I return as a response to the HTTP caller either an "Ok" response, or a "BadRequest" response. If I try a request that is not supposed to work yet, using Postman, the server can't handle my request... so, that is completely correct. But what I don't understand is that my API returns a 500 internal server error response with the whole SQL Exception text as a response to my postman client. I think it is a bad idea to show the whole SQL Exception text message to the client. I could understand why. But why does my API even give this 500 response? I never wrote a 500 response in my controller action. What am I supposed to do to "hide" the Exception text messages? Do I need to write a custom text and return a 500 response manually with this text for every action of every controller, like in a more generic catch (Exception ex) ? What could be considered as best practice in this case? Thank you for reading this, and many thanks for any reply! 😁
4 Replies
Plerx
Plerxβ€’5mo ago
I assume you run it with development as the environment. Most boilerplates add the dev exceptions middleware
Tarkacode
Tarkacodeβ€’5mo ago
Yes I run it in development! The only middleware related to dev env that I see is this :
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
Is this the middleware that add the dev exceptions? In any case, I guess I should not be bothered by these dev exceptions if it only appears in a dev environment?
gwon8266
gwon8266β€’5mo ago
You can write a custom middleware to catch the 500 error then you can return it in whatever style you like and also when you run it in production mode. You should just see, "Internal Server Error" and not the mumbo jumbo
Tarkacode
Tarkacodeβ€’5mo ago
Ok, thank you very much! I will try to make my own middleware, I never made one, it is a good idea to try it myself
Want results from more Discord servers?
Add your server
More Posts
Advice on how to correctly integrate more complex Javascript codeHey, I've developed an ASP.NET app before, but that one was pretty much all about the backend and tβœ… A generic error occurred in GDI+.' -> Save Image PathHey, I been trying to making some face recognition using EMGU.CV, but I faces a problem which, when Navigation and Dialogs with MVVM and WPFI'm using `CommunityToolkit.MVVM` which doesn't include pre-made classes for navigation and dialog iAPI for searchingHello, I'am doing a application for selling product online. And you know, the search bar, I want to how to search .txt file with textboxes and dropdownlists and print in a table or label vs webformsIm trying to search data from a .txt file with textboxes and dropdownlists, then print the item i secant set enhanced mouse precisionOk i still don’t have the mouse thing working. I’m trying to set the enhanced mouse position option Create JSON objects from System.Text.JsonFor a while now I've been trying to get away from dependencies that I don't need anymore, including How to change the button output. Instead of questions it should show some more buttons.``` using System; using System.Linq; using NativeUiLib; //This ads the GUI contols. Minor abstractio.NET 8 error when using ASP.NET AspNetCore.Reporting.LocalReport`var result = localReport.Execute(AspNetCore.Reporting.RenderType.ExcelOpenXml, 1, param, "");` Errcan we send extra data in .net8 identity endpointsi need to send some extra data add the time of register. this register end point only allow email an