Skip to content

Commit

Permalink
feat(templates): improve Boilerplate exception handler's display erro…
Browse files Browse the repository at this point in the history
…r mechanism #9536 (#9537)
  • Loading branch information
ysmoradi authored Dec 23, 2024
1 parent 867e2b3 commit 4e3f667
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,6 @@ private void HandleException(Exception exp,
}
parameters["ComponentType"] = GetType().FullName;

ExceptionHandler.Handle(exp, nonInterrupting: false, parameters, lineNumber, memberName, filePath);
ExceptionHandler.Handle(exp, displayKind: ExceptionDisplayKind.Interrupting, parameters, lineNumber, memberName, filePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async Task RefreshTokenImplementation()
{
{ "AdditionalData", "Refreshing access token failed." },
{ "RefreshTokenRequestedBy", requestedBy }
}, nonInterrupting: exp is ReusedRefreshTokenException);
}, displayKind: exp is ReusedRefreshTokenException ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.Interrupting);

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

var token = await promptService.Show(localizer[AppStrings.EnterElevatedAccessToken], title: "Boilerplate", otpInput: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract partial class ClientExceptionHandlerBase : SharedExceptionHandle
[AutoInject] protected readonly ILogger<ClientExceptionHandlerBase> Logger = default!;

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

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

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

string exceptionMessageToShow = GetExceptionMessageToShow(exception);

if (nonInterrupting)
if (displayKind is ExceptionDisplayKind.NonInterrupting)
{
SnackBarService.Error("Boilerplate", exceptionMessageToShow);
}
else
else if (displayKind is ExceptionDisplayKind.Interrupting)
{
MessageBoxService.Show(exceptionMessageToShow, Localizer[nameof(AppStrings.Error)]);
}

if (isDevEnv)
else if (displayKind is ExceptionDisplayKind.None && isDevEnv)
{
Debugger.Break();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@

namespace Boilerplate.Client.Core.Services.Contracts;

public enum ExceptionDisplayKind
{
/// <summary>
/// No error message is shown to the user.
/// </summary>
None,
/// <summary>
/// Requires the user to acknowledge the error (e.g., by tapping "OK").
/// </summary>
Interrupting,
/// <summary>
/// Shows an auto-dismissed message (e.g., a toast notification)
/// </summary>
NonInterrupting
}

public interface IExceptionHandler
{
void Handle(Exception exception,
bool nonInterrupting = false,
void Handle(Exception exception,
ExceptionDisplayKind displayKind = ExceptionDisplayKind.Interrupting,
Dictionary<string, object?>? parameters = null,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string memberName = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private static void LogException(object? error, string reportedBy)
services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
}, nonInterrupting: true);
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace Boilerplate.Client.Maui.Services;
/// </summary>
public partial class MauiExceptionHandler : ClientExceptionHandlerBase
{
protected override void Handle(Exception exception, bool nonInterrupting, Dictionary<string, object> parameters)
protected override void Handle(Exception exception, ExceptionDisplayKind displayKind, Dictionary<string, object> parameters)
{
exception = UnWrapException(exception);

if (IgnoreException(exception))
return;

base.Handle(exception, nonInterrupting, parameters);
base.Handle(exception, displayKind, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void LogException(object? error, string reportedBy, WebAssemblyHo
services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
}, nonInterrupting: true);
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public partial class WebExceptionHandler : ClientExceptionHandlerBase
{
protected override void Handle(Exception exception, bool nonInterrupting, Dictionary<string, object> parameters)
protected override void Handle(Exception exception, ExceptionDisplayKind displayKind, Dictionary<string, object> parameters)
{
exception = UnWrapException(exception);

if (IgnoreException(exception))
return;

base.Handle(exception, nonInterrupting, parameters);
base.Handle(exception, displayKind, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static void LogException(object? error, string reportedBy)
Services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
}, nonInterrupting: true);
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public partial class WindowsExceptionHandler : ClientExceptionHandlerBase
{
protected override void Handle(Exception exception, bool nonInterrupting, Dictionary<string, object> parameters)
protected override void Handle(Exception exception, ExceptionDisplayKind displayKind, Dictionary<string, object> parameters)
{
exception = UnWrapException(exception);

if (IgnoreException(exception))
return;

base.Handle(exception, nonInterrupting, parameters);
base.Handle(exception, displayKind, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void LogException(object? error, string reportedBy, WebApplicatio
scope.ServiceProvider.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
}, nonInterrupting: true);
}, displayKind: AppEnvironment.IsDev() ? ExceptionDisplayKind.NonInterrupting : ExceptionDisplayKind.None);
}
else
{
Expand Down

0 comments on commit 4e3f667

Please sign in to comment.