Ejemplo n.º 1
0
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            if (!context.Response.HasStarted)
            {
                var         services = context.RequestServices;
                var         executor = services.GetRequiredService <ApiResponseExecutor>();
                ApiResponse response = null;
                if (exception is IApiResponseException e)
                {
                    response = e.GetResponse();
                }
                else
                {
                    response = new UnhandledExceptionResponse(exception);
                }

                executor.RenderResponse(context, response);
            }
            else
            {
                logger.LogCritical("Response already started. Unable to generate a response");
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            if (!context.Response.HasStarted)
            {
                var         services = context.RequestServices;
                var         executor = services.GetRequiredService <ApiResponseExecutor>();
                ApiResponse response = null;
                if (exception is IApiResponseException)
                {
                    switch (exception)
                    {
                    case BadRequestResponseException br:
                        response = BadRequest.Response.GetFromException(br);
                        break;

                    case NoContentResponseException nr:
                        response = NoContent.Response;
                        break;

                    default:     // just in case of something missed
                        logger.LogWarning(FormatSystemMessage("An Api Response Exception {0}({1}) was found", exception.GetType().FullName, exception.Message));
                        IApiResponseException apiException = exception as IApiResponseException;
                        response         = new ApiResponse(apiException.HttpCode, apiException.StatusCode, apiException.Description, apiException.UserDescription);
                        response.HasBody = apiException.HasBody;
                        break;
                    }
                }
                else
                {
                    response = new UnhandledExceptionResponse(exception);
                }

                executor.RenderResponse(context, response);
            }
            else
            {
                logger.LogCritical("Response already started. Unable to generate a response");
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 3
0
        public override void OnException(Microsoft.AspNetCore.Mvc.Filters.ExceptionContext context)
        {
            var exception = context.Exception;

            var exceptionType = exception.GetType();

            if (!exceptionHandlers.ContainsKey(exceptionType))
            {
                var request  = context.HttpContext.Request;
                var response = new UnhandledExceptionResponse
                {
                    Message = context.Exception.ToString()
                };
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Result = new JsonResult(response);
            }
            else
            {
                context.Result = exceptionHandlers[exceptionType](context, exception);
            }
            base.OnException(context);
        }
Ejemplo n.º 4
0
        public void OnException(ExceptionContext context)
        {
            ResponseBase response;
            JsonResult   result;

            if (context.Exception is ResponseException)
            {
                ResponseException ex = context.Exception as ResponseException;
                response = new ResponseBase(context.HttpContext)
                {
                    _StatusCode        = "KO_" + ex.KoSubCode,
                    _StatusDescription = ex.Description
                };
                if (context.Exception.InnerException != null)
                {
                    response.SetExceptionInfo(context.Exception.InnerException, false);
                }
                result            = new JsonResult(response);
                result.StatusCode = ex.HttpCode;
            }
            else
            {
                response          = new UnhandledExceptionResponse(context.HttpContext, context.Exception);
                result            = new JsonResult(response);
                result.StatusCode = 500;
            }
            context.ExceptionHandled = true;
            context.Result           = result;

            try
            {
                Logger.LogError("Message: " + context.Exception.Message + " Stack Trace: " + context.Exception.StackTrace);
            }
            catch (Exception)
            {
            }
        }