Exemple #1
0
        public static void UseExceptionTransformer(this IApplicationBuilder builder)
        {
            ExceptionTransformerFactory factory = builder
                                                  .ApplicationServices.GetService <ExceptionTransformerFactory>();

            List <IExceptionTransformer> transformers = builder.ApplicationServices.GetServices <IExceptionTransformer>().ToList();

            foreach (var transformer in transformers)
            {
                factory.AddTransformer(transformer);
            }

            builder.UseMiddleware <ExceptionTransformerMiddleware>();
        }
        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
            };
        }
Exemple #3
0
 public ExceptionTransformerMiddleware(RequestDelegate nextDelegate, ExceptionTransformerFactory factory)
 {
     this.nextDelegate = nextDelegate;
     this.factory      = factory;
 }