Exemple #1
0
        private async Task HandleException(HttpContext context, System.Exception originalException)
        {
            if (context.Response.HasStarted)
            {
                var exceptionName = originalException.GetType().FullName;

                logger.LogError
                (
                    originalException,
                    "The response has already started so exception {ExceptionName} with message {ExceptionMessage} cannot be handled",
                    exceptionName,
                    originalException.Message
                );

                return;
            }

            context.Response.Clear();

            var     exceptionHandlerType = SExceptionHandlerInterfaceType.MakeGenericType(originalException.GetType());
            dynamic exceptionHandler;

            try
            {
                exceptionHandler = componentContext.Resolve(exceptionHandlerType);
            }
            catch (System.Exception exception)
            {
                var exceptionName         = exception.GetType().FullName;
                var originalExceptionName = originalException.GetType().FullName;

                logger.LogWarning
                (
                    exception,
                    "Exception {ExceptionName} thrown with message {ExceptionMessage} when resolving exception handler for exception {OriginalExceptionName} with message {OriginalExceptionMessage}",
                    exceptionName,
                    exception.Message,
                    originalExceptionName,
                    originalException.Message
                );

                await HandleUsingDefaultExceptionHandler(context, originalException);

                return;
            }

            var handleMethod = exceptionHandlerType.GetTypeInfo().GetMethod("HandleAsync");

            try
            {
                await handleMethod.Invoke(exceptionHandler, new object[] { context, originalException });
            }
            catch (System.Exception exception)
            {
                var exceptionName         = exception.GetType().FullName;
                var originalExceptionName = originalException.GetType().FullName;

                logger.LogError
                (
                    exception,
                    "Exception {ExceptionName} thrown with message {ExceptionMessage} when handling exception {OriginalExceptionName} with message {OriginalExceptionMessage}",
                    exceptionName,
                    exception.Message,
                    originalExceptionName,
                    originalException.Message
                );

                if (hostingEnvironment.IsDevelopment())
                {
                    var response = new ExceptionResponse
                                   (
                        "ExceptionHandlerThrewException",
                        $"Exception {exceptionName} thrown with message {exception.Message} when handling exception {originalExceptionName} with message {originalException.Message}",
                        new Exception(exception)
                                   );

                    await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response, JsonConstants.JsonSerializerSettings);
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
            }
        }
Exemple #2
0
        private async Task HandleUsingDefaultExceptionHandler(HttpContext context, System.Exception originalException)
        {
            var originalExceptionName = originalException.GetType().FullName;

            if (originalException.GetType() == typeof(TimeoutException))
            {
                logger.LogCritical
                (
                    originalExceptionName,
                    "Unhandled exception {ExceptionName} thrown with message {ExceptionMessage}",
                    originalExceptionName,
                    originalException.Message
                );
            }
            else
            {
                logger.LogError
                (
                    originalException,
                    "Unhandled exception {ExceptionName} thrown with message {ExceptionMessage}",
                    originalExceptionName,
                    originalException.Message
                );
            }

            var version = context.GetRequestedApiVersion()?.MajorVersion ?? Versions.Latest;

            IDefaultExceptionHandler defaultExceptionHandler;

            try
            {
                defaultExceptionHandler = componentContext.ResolveVersioned <IDefaultExceptionHandler>(version);
            }
            catch (System.Exception exception)
            {
                var exceptionName = exception.GetType().FullName;

                logger.LogError
                (
                    exception,
                    "Exception {ExceptionName} thrown with message {ExceptionMessage} when resolving default exception handler for exception {OriginalExceptionName} with message {OriginalExceptionMessage} for version {Version}",
                    exceptionName,
                    exception.Message,
                    originalExceptionName,
                    originalException.Message,
                    version
                );

                if (hostingEnvironment.IsDevelopment())
                {
                    var response = new ExceptionResponse
                                   (
                        "DefaultExceptionHandlerNotRegistered",
                        $"Exception {exceptionName} thrown with message {exception.Message} when resolving default exception handler for exception {originalExceptionName} with message {originalException.Message} for version {version}",
                        new Exception(exception)
                                   );

                    await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response, JsonConstants.JsonSerializerSettings);
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }

                return;
            }

            try
            {
                await defaultExceptionHandler.HandleAsync(context, originalException);
            }
            catch (System.Exception exception)
            {
                var exceptionName = exception.GetType().FullName;

                logger.LogError
                (
                    exception,
                    "Exception {ExceptionName} thrown with message {ExceptionMessage} when handling exception {OriginalExceptionName} with message {OriginalExceptionMessage}",
                    exceptionName,
                    exception.Message,
                    originalExceptionName,
                    originalException.Message
                );

                if (hostingEnvironment.IsDevelopment())
                {
                    var response = new ExceptionResponse
                                   (
                        "DefaultExceptionHandlerThrewException",
                        $"Exception {exceptionName} thrown with message {exception.Message} when handling exception {originalExceptionName} with message {originalException.Message}",
                        new Exception(exception)
                                   );

                    await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response, JsonConstants.JsonSerializerSettings);
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
            }
        }