Exception Handler
Exception Handling
Exception handling across all handler types is easier than ever with a exception handler. Global exception handling is nothing more than middleware installing middleware for all handler types, but providing one interface to rule them. Best of all, you don’t have to use 1 exception handler to cover all cases. You can register multiple and just return true if your handler managed the exception.
public class MyExceptionHandler : Shiny.Mediator.IExceptionHandler{ public Task<bool> Handle(Exception ex, EventContext context) { // do something return Task.FromResult(true); }}
// can be registered as scoped or singletonservices.AddShinyMediator(x => x.AddExceptionHandler<MyExceptionHandler>());
Global Exception Handling middleware is added automatically by the library This middleware does not apply to Streams at this time
Prevent Event Exceptions
Events can throw exceptions. You can trap them at a global level by awaiting the Publish call. However, there may be cases where you may want other events to complete and not be blocked by one misbehaving piece of code. Thus, we have middleware to help deal with this in a very simple/straight forward manor.
This middleware does nothing more than log an error and moves on
. This is applied to all event handlers and does not require any attributes
or configuration like other middleware
User Notification Exception Handling
User notification exception handling is great for scenarios where the user initiates an action, but an issue occurs such as the network failing, data issues, etc. This middleware does the standard try/execute/catch/log & notify user that something went wrong
Usage
To use it on with your request handlers, simply configured the UserErrorNotifications
section in your appsettings.json
file:
{ "Mediator": { "UserErrorNotifications": { "My.Contracts.*": { "*": { "Title": "ERROR", "Message" : "Failed to do something" }, "fr-CA": { "Title": "ERREUR", "Message" : "Échec de faire quelque chose" } } } }}