/// <summary>
        /// Creates an instance of the options.
        /// </summary>
        public ExceptionMappingServicesOptions()
        {
            Schemes = new Dictionary <string, ExceptionMappingSchemeRegistration>(StringComparer.Ordinal);

            // By default, unhandled exception is rethrown
            _fallbackHandler = ctx =>
            {
                ctx.Result = ExceptionHandlingResult.Rethrow(ctx.Exception);
                return(default);
Beispiel #2
0
        public async Task <ExceptionHandlingResult> MapAsync(
            Exception exception,
            ExceptionMappingContext?context)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            context ??= new ExceptionMappingContext();
            try
            {
                foreach (var scheme in _schemeProvider.GetSchemes())
                {
                    var handler = await _handlerProvider.GetHandlerAsync(scheme);

                    var result = await handler.HandleExceptionAsync(exception, context);

                    if (result.IsHandledSuccessfully)
                    {
                        return(result);
                    }
                }

                var fallbackContext = new ExceptionHandlingContext(exception, context);
                await _options.FallbackExceptionHandler(fallbackContext);

                return(fallbackContext.Result);
            }
            catch (Exception ex)
            {
                // An unhandled exception occurred while executing the handler
                // This is not expected as handlers should be exception-free. When this happens, log the exception
                // and re-throw the *original* exception
                _logger.LogTrace(ex, "Unhandled exception occurred in handlers.");
            }

            return(ExceptionHandlingResult.Rethrow(exception));
        }