Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Created March 6, 2025 16:01
Show Gist options
  • Save pedroinfo/3cb10533f36eabed51a21c77dc0214b5 to your computer and use it in GitHub Desktop.
Save pedroinfo/3cb10533f36eabed51a21c77dc0214b5 to your computer and use it in GitHub Desktop.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage(); // Exibe erros detalhados em desenvolvimento
}
else
{
// Em produção, você pode optar por exibir detalhes de erro
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "application/json";
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature?.Error;
var errorResponse = new
{
Message = "Ocorreu um erro interno no servidor.",
Detail = exception?.Message, // Inclui detalhes do erro
StackTrace = env.IsDevelopment() ? exception?.StackTrace : null // Opcional: inclui stack trace apenas em desenvolvimento
};
await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(errorResponse));
});
});
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment