Esempio n. 1
0
        private async Task HandleWithDefaultHandlerAsync(HttpContext context, Exception exception)
        {
            string originalExceptionName = exception.GetType().FullName;

            try
            {
                await this.defaultExceptionHandler.HandleAsync(context, exception);
            }
            catch (Exception handlerException)
            {
                string exceptionName = handlerException.GetType().FullName;

                if (!this.hostEnvironment.IsProduction())
                {
                    var response = new ExceptionResponse <Exception>(
                        "DefaultExceptionHandlerThrewException",
                        $"Exception {exceptionName} thrown with message {handlerException.Message} when handling exception {originalExceptionName} with message {exception.Message}",
                        handlerException);

                    await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response);
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
            }
        }
Esempio n. 2
0
        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            if (context.Response.HasStarted)
            {
                return;
            }

            context.Response.Clear();

            Type    exceptionHandlerType = ExceptionHandlerInterfaceType.MakeGenericType(exception.GetType());
            dynamic exceptionHandler;

            try
            {
                exceptionHandler = this.serviceProvider.GetService(exceptionHandlerType);
            }
            catch (Exception)
            {
                await this.HandleWithDefaultHandlerAsync(context, exception);

                return;
            }

            if (exceptionHandler == null)
            {
                await this.HandleWithDefaultHandlerAsync(context, exception);

                return;
            }

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

            try
            {
                if (!(handleMethod is null))
                {
                    await handleMethod.Invoke(exceptionHandler, new object[] { context, exception });
                }
            }
            catch (Exception handleException)
            {
                string exceptionName         = handleException.GetType().FullName;
                string originalExceptionName = exception.GetType().FullName;

                if (!this.hostEnvironment.IsProduction())
                {
                    var response = new ExceptionResponse <Exception>(
                        "ExceptionHandlerThrewException",
                        $"Exception {exceptionName} thrown with message {handleException.Message} when handling exception {originalExceptionName} with message {exception.Message}",
                        handleException);

                    await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response);
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
            }
        }
 /// <summary>
 /// Handles the specified exception for the given context.
 /// </summary>
 /// <param name="context">The request context.</param>
 /// <param name="exception">The exception thrown.</param>
 /// <returns>An asynchronous operation.</returns>
 public async Task HandleAsync(HttpContext context, Exception exception)
 {
     var response = new ExceptionResponse <Exception>("UnhandledException", "An unhandled exception occurred.", exception);
     await context.Response.WriteJsonAsync(HttpStatusCode.InternalServerError, response);
 }