![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>
56 lines
1.0 KiB
Plaintext
56 lines
1.0 KiB
Plaintext
== How to fix it in Blazor
|
|
|
|
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=2,diff-type=noncompliant]
|
|
----
|
|
@page "/"
|
|
|
|
<p>@Content</p> <!-- Noncompliant -->
|
|
|
|
@code {
|
|
private String Content = "";
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
try
|
|
{
|
|
throw new InvalidOperationException("");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Content = ex.StackTrace;
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=2,diff-type=compliant]
|
|
----
|
|
@page "/"
|
|
@using Microsoft.Extensions.Logging
|
|
@inject ILogger<Example> Logger
|
|
|
|
<p>Internal Server Error</p>
|
|
|
|
@code {
|
|
protected override void OnInitialized()
|
|
{
|
|
try
|
|
{
|
|
throw new InvalidOperationException("");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError(ex.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
----
|