예제 #1
0
 public async Task Invoke(HttpContext context)
 {
     try
     {
         await nextDelegate.Invoke(context);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         IExceptionTransformer transformer = factory.GetExceptionTransformer(e.GetType());
         ErrorResponseModel    response    = transformer.BuildErrorModel(e);
         transformer.EditContext(context, e);
         context.Response.StatusCode  = response.StatusCode;
         context.Response.ContentType = "application/json";
         await context.Response.WriteAsync(JsonConvert.SerializeObject(response));
     }
 }
        public override void OnException(ExceptionContext context)
        {
            ExceptionTransformerFactory factory = context.HttpContext.RequestServices.
                                                  GetService(typeof(ExceptionTransformerFactory)) as ExceptionTransformerFactory;
            Exception ex = context.Exception;

            Console.Error.WriteLine(ex);

            IExceptionTransformer transformer = factory.GetExceptionTransformer(ex.GetType());

            transformer.EditContext(context.HttpContext, ex);
            ErrorResponseModel error = transformer.BuildErrorModel(ex);

            context.Result = new ObjectResult(error)
            {
                StatusCode = error.StatusCode
            };
        }