Description
Keep exception messages, stack traces and inner exceptions out of client error responses.
Potential impact
Exception details can reveal server paths, internal types or where input validation failed.
Remediation
Use an appropriate status code and a generic message in the response. Keep the diagnostic details in access-controlled server logs.
Examples
Before
csharp
return BadRequest(new { error = ex.Message });
After
csharp
logger.LogError(ex, "Request failed");
return BadRequest(new { error = "Request failed" });
Both snippets return 400. The revision logs ex on the server and sends only Request failed as the error message.