Created
March 6, 2025 16:01
-
-
Save pedroinfo/3cb10533f36eabed51a21c77dc0214b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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