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 }; }
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); } }); }); }
public static IServiceCollection AppAddIoCServices(this IServiceCollection services, IConfiguration config) { //options/config ConfigureOptions(services, config); //Infra AppLoggerFactory.Initialize(new AppLogger(), config.GetConnectionString("Connection")); services.AddSingleton(AppLoggerFactory.GetLogger()); services.AddSingleton <IAppHttpClient, AppHttpClient>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <ILoggedUser, LoggedUser>(); //events services.AddScoped <IDomainNotification, DomainNotification>(); //services services.AddScoped <IPasswordHasherService, PasswordHasherService>(); //validators services.AddValidatorsFromAssembly(typeof(CreateUserCommandValidator).Assembly); //persistence services.AddScoped <IUnitOfWork, UnitOfWork>(); typeof(UserRepository).Assembly.GetTypes().Where(x => x.FullName.Contains("Repositories") && x.GetInterfaces().Any() && x.IsClass && x != typeof(Repository <>)) .ToList().ForEach(x => { var @interface = x.GetInterfaces().FirstOrDefault(s => s.Name.Contains(x.Name)); if (@interface == null) { return; } services.AddScoped(@interface, x); }); return(services); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) { app.UseDeveloperExceptionPage(); app.UseStaticFiles(); app.UseResponseCompression(); app.ConfigureExceptionHandler(); app.UseAuthentication(); app.UseRouting(); app.UseCors(CorsPolicy); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.AppUseApiDocs(); app.AppUseMigrations(Configuration, env); AppLoggerFactory.GetLogger().Info("Build Realizado com sucesso"); }