예제 #1
0
        public override void OnException(ExceptionContext context)
        {
            var notification = new Notification("Ocorreu um erro interno");

            var domainNotification =
                context.HttpContext.RequestServices.GetService(typeof(IDomainNotification)) as IDomainNotification;

            AppLoggerFactory.GetLogger().Error(context.Exception);

            if (domainNotification == null)
            {
                context.Result = new ObjectResult(EnvelopResult.Fail(new[] { notification }))
                {
                    StatusCode = 500
                };
                return;
            }

            if (!domainNotification.HasNotifications())
            {
                domainNotification.Handle(notification);
            }

            context.Result = new ObjectResult(EnvelopResult.Fail(domainNotification.Notifications))
            {
                StatusCode = 500
            };
        }
예제 #2
0
 public IActionResult CreatedResponse <T>(T data = default, string url = "")
 {
     if (!DomainNotification.HasNotifications())
     {
         return(Created(url, EnvelopDataResult <T> .Ok(data)));
     }
     return(BadRequest(EnvelopResult.Fail(DomainNotification.Notify())));
 }
예제 #3
0
 public IActionResult CreatedResponse(string url = "")
 {
     if (!DomainNotification.HasNotifications())
     {
         return(Created(url, EnvelopResult.Ok()));
     }
     return(BadRequest(EnvelopResult.Fail(DomainNotification.Notify())));
 }
예제 #4
0
 public IActionResult CreateResponse()
 {
     if (!DomainNotification.HasNotifications())
     {
         return(Ok(EnvelopResult.Ok()));
     }
     return(BadRequest(EnvelopResult.Fail(DomainNotification.Notify())));
 }
예제 #5
0
        public IActionResult NotFoundResponse()
        {
            if (DomainNotification.HasNotifications())
            {
                DomainNotification.Dispose();
            }

            DomainNotification.Handle(new Notification("Recurso não encontrado"));
            return(new NotFoundObjectResult(EnvelopResult.Fail(DomainNotification.Notify())));
        }
예제 #6
0
        public IActionResult UnprocessableResponse()
        {
            if (DomainNotification.HasNotifications())
            {
                DomainNotification.Dispose();
            }

            DomainNotification.Handle(new Notification("Entidade não processável"));

            return(UnprocessableEntity(EnvelopResult.Fail(DomainNotification.Notify())));
        }
예제 #7
0
        public IActionResult ForbiddenResponse()
        {
            if (DomainNotification.HasNotifications())
            {
                DomainNotification.Dispose();
            }

            DomainNotification.Handle(new Notification("Forbidden"));

            return(new ObjectResult(EnvelopResult.Fail(DomainNotification.Notify()))
            {
                StatusCode = 403
            });
        }
예제 #8
0
        public IActionResult UnauthorizedResponse()
        {
            if (DomainNotification.HasNotifications())
            {
                DomainNotification.Dispose();
            }

            DomainNotification.Handle(new Notification("Unauthorized"));

            return(new ObjectResult(EnvelopResult.Fail(DomainNotification.Notify()))
            {
                StatusCode = 401
            });
        }
예제 #9
0
        public static void ConfigureExceptionHandler(this IApplicationBuilder app)
        {
            app.UseExceptionHandler(appError =>
            {
                appError.Run(async context =>
                {
                    var contextFeature = context.Features.Get <IExceptionHandlerFeature>();

                    if (contextFeature != null)
                    {
                        if (contextFeature.Error is InvalidOperationException &&
                            contextFeature.Error.Message.StartsWith(
                                "The SPA default page middleware could not return the default page"))
                        {
                            return;
                        }

                        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                        EnvelopResult result = null;

                        var notification = new Notification("Ocorreu um erro interno");

                        var domainNotification =
                            context.RequestServices.GetService(typeof(IDomainNotification)) as IDomainNotification;

                        AppLoggerFactory.GetLogger().Error(contextFeature.Error);

                        if (domainNotification == null)
                        {
                            result = EnvelopResult.Fail(new[] { notification });
                        }

                        if (domainNotification?.HasNotifications() == false)
                        {
                            domainNotification.Handle(notification);
                            result = EnvelopResult.Fail(domainNotification?.Notifications);
                        }

                        var response = JsonUtils.Serialize(result);

                        await context.Response.WriteAsync(response);
                    }
                });
            });
        }