-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRaygunErrorBoundary.cs
More file actions
110 lines (90 loc) · 3.85 KB
/
RaygunErrorBoundary.cs
File metadata and controls
110 lines (90 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
namespace Raygun.Blazor.WebAssembly.Controls
{
/// <summary>
/// An extension of the Blazor <see cref="ErrorBoundary" /> control that automatically sends exceptions to Raygun.
/// </summary>
public class RaygunErrorBoundary : ErrorBoundary
{
#region Internal Parameters
/// <summary>
///
/// </summary>
[Inject]
internal IJSRuntime JSRuntime { get; set; }
/// <summary>
///
/// </summary>
[Inject]
internal RaygunBlazorClient RaygunClient { get; set; }
/// <summary>
///
/// </summary>
[Inject]
internal IOptions<RaygunSettings> RaygunSettings { get; set; }
#endregion
#region Public Parameters
/// <summary>
///
/// </summary>
[Parameter]
public bool ShowExceptionUI { get; set; }
#endregion
#region Internal Methods
/// <summary>
/// When an error occurs, send it to Raygun.
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
protected override async Task OnErrorAsync(Exception exception)
{
// Always call to parent OnErrorAsync to log the error with default logger.
await base.OnErrorAsync(exception);
if (!RaygunSettings.Value.CatchUnhandledExceptions) return;
await RaygunClient.RecordExceptionAsync(exception, null, ["UnhandledException", "Blazor", ".NET"]);
}
/// <inheritdoc />
/// <remarks>
/// We are rendering differently than the ErrorBoundary base control because Raygun's ethos is to first not
/// mess with anything about the app. So if the developer wants to display UI, they have to specifically opt-in.
/// </remarks>
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (CurrentException is not null && ShowExceptionUI)
{
if (ErrorContent is not null)
{
builder.AddContent(1, ErrorContent(CurrentException));
}
else
{
// RWM: We may need to consider invoking JavaScript to set the "blazor-error-ui" visible instead.
// The code sets the style to display-block;
// The default error UI doesn't include any content, because:
// [1] We don't know whether or not you'd be happy to show the stack trace. It depends both on
// whether DetailedErrors is enabled and whether you're in production, because even on WebAssembly
// you likely don't want to put technical data like that in the UI for end users. A reasonable way
// to toggle this is via something like "#if DEBUG" but that can only be done in user code.
// [2] We can't have any other human-readable content by default, because it would need to be valid
// for all languages.
// Instead, the default project template provides locale-specific default content via CSS. This provides
// a quick form of customization even without having to subclass this component.
builder.OpenElement(2, "div");
builder.AddAttribute(3, "class", "blazor-error-boundary");
builder.CloseElement();
}
}
else
{
builder.AddContent(0, ChildContent);
}
}
#endregion
}
}