Skip to content

Commit 4e3f667

Browse files
authored
feat(templates): improve Boilerplate exception handler's display error mechanism #9536 (#9537)
1 parent 867e2b3 commit 4e3f667

File tree

11 files changed

+37
-22
lines changed

11 files changed

+37
-22
lines changed

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/AppComponentBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,6 @@ private void HandleException(Exception exp,
260260
}
261261
parameters["ComponentType"] = GetType().FullName;
262262

263-
ExceptionHandler.Handle(exp, nonInterrupting: false, parameters, lineNumber, memberName, filePath);
263+
ExceptionHandler.Handle(exp, displayKind: ExceptionDisplayKind.Interrupting, parameters, lineNumber, memberName, filePath);
264264
}
265265
}

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AuthManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ async Task RefreshTokenImplementation()
128128
{
129129
{ "AdditionalData", "Refreshing access token failed." },
130130
{ "RefreshTokenRequestedBy", requestedBy }
131-
}, nonInterrupting: exp is ReusedRefreshTokenException);
131+
}, displayKind: exp is ReusedRefreshTokenException ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.Interrupting);
132132

133133
if (exp is UnauthorizedException // refresh token is also invalid.
134134
|| exp is ReusedRefreshTokenException && refreshToken == await storageService.GetItem("refresh_token"))
@@ -182,7 +182,7 @@ public async Task<bool> TryEnterElevatedAccessMode(CancellationToken cancellatio
182182
}
183183
catch (TooManyRequestsExceptions exp)
184184
{
185-
exceptionHandler.Handle(exp, nonInterrupting: true); // Let's show prompt anyway.
185+
exceptionHandler.Handle(exp, displayKind: ExceptionDisplayKind.NonInterrupting); // Let's show prompt anyway.
186186
}
187187

188188
var token = await promptService.Show(localizer[AppStrings.EnterElevatedAccessToken], title: "Boilerplate", otpInput: true);

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ClientExceptionHandlerBase.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract partial class ClientExceptionHandlerBase : SharedExceptionHandle
1212
[AutoInject] protected readonly ILogger<ClientExceptionHandlerBase> Logger = default!;
1313

1414
public void Handle(Exception exception,
15-
bool nonInterrupting = false,
15+
ExceptionDisplayKind displayKind = ExceptionDisplayKind.Interrupting,
1616
Dictionary<string, object?>? parameters = null,
1717
[CallerLineNumber] int lineNumber = 0,
1818
[CallerMemberName] string memberName = "",
@@ -25,11 +25,11 @@ public void Handle(Exception exception,
2525
parameters[nameof(lineNumber)] = lineNumber;
2626
parameters["exceptionId"] = Guid.NewGuid(); // This will remain consistent across different registered loggers, such as Sentry, Application Insights, etc.
2727

28-
Handle(exception, nonInterrupting, parameters.ToDictionary(i => i.Key, i => i.Value ?? string.Empty));
28+
Handle(exception, displayKind, parameters.ToDictionary(i => i.Key, i => i.Value ?? string.Empty));
2929
}
3030

3131
protected virtual void Handle(Exception exception,
32-
bool nonInterrupting,
32+
ExceptionDisplayKind displayKind,
3333
Dictionary<string, object> parameters)
3434
{
3535
var isDevEnv = AppEnvironment.IsDev();
@@ -50,16 +50,15 @@ protected virtual void Handle(Exception exception,
5050

5151
string exceptionMessageToShow = GetExceptionMessageToShow(exception);
5252

53-
if (nonInterrupting)
53+
if (displayKind is ExceptionDisplayKind.NonInterrupting)
5454
{
5555
SnackBarService.Error("Boilerplate", exceptionMessageToShow);
5656
}
57-
else
57+
else if (displayKind is ExceptionDisplayKind.Interrupting)
5858
{
5959
MessageBoxService.Show(exceptionMessageToShow, Localizer[nameof(AppStrings.Error)]);
6060
}
61-
62-
if (isDevEnv)
61+
else if (displayKind is ExceptionDisplayKind.None && isDevEnv)
6362
{
6463
Debugger.Break();
6564
}

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/Contracts/IExceptionHandler.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22

33
namespace Boilerplate.Client.Core.Services.Contracts;
44

5+
public enum ExceptionDisplayKind
6+
{
7+
/// <summary>
8+
/// No error message is shown to the user.
9+
/// </summary>
10+
None,
11+
/// <summary>
12+
/// Requires the user to acknowledge the error (e.g., by tapping "OK").
13+
/// </summary>
14+
Interrupting,
15+
/// <summary>
16+
/// Shows an auto-dismissed message (e.g., a toast notification)
17+
/// </summary>
18+
NonInterrupting
19+
}
20+
521
public interface IExceptionHandler
622
{
7-
void Handle(Exception exception,
8-
bool nonInterrupting = false,
23+
void Handle(Exception exception,
24+
ExceptionDisplayKind displayKind = ExceptionDisplayKind.Interrupting,
925
Dictionary<string, object?>? parameters = null,
1026
[CallerLineNumber] int lineNumber = 0,
1127
[CallerMemberName] string memberName = "",

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private static void LogException(object? error, string reportedBy)
201201
services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
202202
{
203203
{ nameof(reportedBy), reportedBy }
204-
}, nonInterrupting: true);
204+
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
205205
}
206206
else
207207
{

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Services/MauiExceptionHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Boilerplate.Client.Maui.Services;
99
/// </summary>
1010
public partial class MauiExceptionHandler : ClientExceptionHandlerBase
1111
{
12-
protected override void Handle(Exception exception, bool nonInterrupting, Dictionary<string, object> parameters)
12+
protected override void Handle(Exception exception, ExceptionDisplayKind displayKind, Dictionary<string, object> parameters)
1313
{
1414
exception = UnWrapException(exception);
1515

1616
if (IgnoreException(exception))
1717
return;
1818

19-
base.Handle(exception, nonInterrupting, parameters);
19+
base.Handle(exception, displayKind, parameters);
2020
}
2121
}

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private static void LogException(object? error, string reportedBy, WebAssemblyHo
8080
services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
8181
{
8282
{ nameof(reportedBy), reportedBy }
83-
}, nonInterrupting: true);
83+
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
8484
}
8585
else
8686
{

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Services/WebExceptionHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
public partial class WebExceptionHandler : ClientExceptionHandlerBase
44
{
5-
protected override void Handle(Exception exception, bool nonInterrupting, Dictionary<string, object> parameters)
5+
protected override void Handle(Exception exception, ExceptionDisplayKind displayKind, Dictionary<string, object> parameters)
66
{
77
exception = UnWrapException(exception);
88

99
if (IgnoreException(exception))
1010
return;
1111

12-
base.Handle(exception, nonInterrupting, parameters);
12+
base.Handle(exception, displayKind, parameters);
1313
}
1414
}

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private static void LogException(object? error, string reportedBy)
129129
Services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
130130
{
131131
{ nameof(reportedBy), reportedBy }
132-
}, nonInterrupting: true);
132+
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
133133
}
134134
else
135135
{

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Services/WindowsExceptionHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
public partial class WindowsExceptionHandler : ClientExceptionHandlerBase
44
{
5-
protected override void Handle(Exception exception, bool nonInterrupting, Dictionary<string, object> parameters)
5+
protected override void Handle(Exception exception, ExceptionDisplayKind displayKind, Dictionary<string, object> parameters)
66
{
77
exception = UnWrapException(exception);
88

99
if (IgnoreException(exception))
1010
return;
1111

12-
base.Handle(exception, nonInterrupting, parameters);
12+
base.Handle(exception, displayKind, parameters);
1313
}
1414
}

0 commit comments

Comments
 (0)