![github-actions[bot]](/assets/img/avatar_default.png)
* Add csharp to rule S6776 * Add blazor content * Add Blazor * Add how to fix it in ASP.NET section * Update rules/S6776/csharp/how-to-fix-it/blazor.adoc Co-authored-by: Loris S. <91723853+loris-s-sonarsource@users.noreply.github.com> * Update rules/S6776/csharp/how-to-fix-it/blazor.adoc Co-authored-by: Loris S. <91723853+loris-s-sonarsource@users.noreply.github.com> * Remove dash --------- Co-authored-by: hendrik-buchwald-sonarsource <hendrik-buchwald-sonarsource@users.noreply.github.com> Co-authored-by: Hendrik Buchwald <hendrik.buchwald@sonarsource.com> Co-authored-by: Daniel Teuchert <daniel.teuchert@sonarsource.com> Co-authored-by: daniel-teuchert-sonarsource <141642369+daniel-teuchert-sonarsource@users.noreply.github.com> Co-authored-by: Loris S. <91723853+loris-s-sonarsource@users.noreply.github.com>
57 lines
1.2 KiB
Plaintext
57 lines
1.2 KiB
Plaintext
== How to fix it in ASP.NET
|
|
|
|
Implement proper error handling by reporting customized error messages that do not contain a detailed stack trace. Log the exception stack trace if needed.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
[ApiController]
|
|
[Route("/")]
|
|
public class StacktraceController : ControllerBase
|
|
{
|
|
[HttpGet("Exception")]
|
|
public string ExceptionEndpoint()
|
|
{
|
|
try {
|
|
throw new InvalidOperationException(ExceptionMessage);
|
|
}
|
|
catch (Exception ex) {
|
|
return ex.StackTrace; // Noncompliant
|
|
}
|
|
return "Ok";
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
[ApiController]
|
|
[Route("/")]
|
|
public class StacktraceController : ControllerBase
|
|
{
|
|
private readonly ILogger<StacktraceController> Logger;
|
|
|
|
public StacktraceController(ILogger<StacktraceController> logger)
|
|
{
|
|
Logger = logger;
|
|
}
|
|
|
|
[HttpGet("Exception")]
|
|
public string ExceptionEndpoint()
|
|
{
|
|
try {
|
|
throw new InvalidOperationException(ExceptionMessage);
|
|
}
|
|
catch (Exception ex) {
|
|
Logger.LogError(ex.StackTrace);
|
|
}
|
|
return "Ok";
|
|
}
|
|
}
|
|
----
|