-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
68 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...erplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Services/WebExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/SharedExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Reflection; | ||
|
||
namespace Boilerplate.Shared.Services; | ||
|
||
public partial class SharedExceptionHandler | ||
{ | ||
[AutoInject] protected IStringLocalizer<AppStrings> Localizer { get; set; } = default!; | ||
|
||
protected string GetExceptionMessageToShow(Exception exception) | ||
{ | ||
if (exception is KnownException) | ||
return exception.Message; | ||
|
||
if (AppEnvironment.IsDev()) | ||
return exception.ToString(); | ||
|
||
return Localizer[nameof(AppStrings.UnknownException)]; | ||
} | ||
|
||
protected string GetExceptionMessageToLog(Exception exception) | ||
{ | ||
var exceptionMessageToLog = exception.Message; | ||
var innerException = exception.InnerException; | ||
|
||
while (innerException is not null) | ||
{ | ||
exceptionMessageToLog += $"{Environment.NewLine}{innerException.Message}"; | ||
innerException = innerException.InnerException; | ||
} | ||
|
||
return exceptionMessageToLog; | ||
} | ||
|
||
protected Exception UnWrapException(Exception exception) | ||
{ | ||
if (exception is AggregateException aggregateException) | ||
{ | ||
return aggregateException.Flatten().InnerException ?? aggregateException; | ||
} | ||
else if (exception is TargetInvocationException) | ||
{ | ||
return exception.InnerException ?? exception; | ||
} | ||
|
||
return exception; | ||
} | ||
|
||
protected bool IgnoreException(Exception exception) | ||
{ | ||
if (exception is KnownException) | ||
return false; | ||
|
||
return exception is TaskCanceledException || | ||
exception is OperationCanceledException || | ||
exception is TimeoutException || | ||
(exception.InnerException is not null && IgnoreException(exception.InnerException)); | ||
} | ||
} |