feat(apis): supply basic/consistent error response

This commit is contained in:
Collin M. Barrett 2021-11-03 17:32:17 -05:00
parent 006a066cd1
commit 5fad42ba1f
4 changed files with 66 additions and 14 deletions

View file

@ -0,0 +1,31 @@
using System;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace FilterLists.Archival.Api.Controllers
{
[ApiController]
[ApiExplorerSettings(IgnoreApi = true)]
// TODO: de-duplicate into SharedKernel
public class ErrorController : ControllerBase
{
[Route("/error-local-development")]
public IActionResult ErrorLocalDevelopment([FromServices] IWebHostEnvironment webHostEnvironment)
{
if (webHostEnvironment.EnvironmentName != "Development")
{
throw new InvalidOperationException("This shouldn't be invoked in non-development environments.");
}
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
return Problem(context.Error.StackTrace, title: context.Error.Message);
}
[Route("/error")]
public IActionResult Error()
{
return Problem();
}
}
}

View file

@ -19,8 +19,7 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(o => o.LowercaseUrls = true);
services.AddControllers()
.AddJsonOptions(o => o.JsonSerializerOptions.IgnoreNullValues = true);
services.AddControllers().AddJsonOptions(o => o.JsonSerializerOptions.IgnoreNullValues = true);
services.AddSwaggerGen();
services.AddApplicationServices(Configuration);
}
@ -28,11 +27,7 @@ public void ConfigureServices(IServiceCollection services)
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseApplication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseExceptionHandler(env.IsDevelopment() ? "/error-local-development" : "/error");
app.UseRouting();
app.UseEndpoints(e => e.MapControllers());
app.UseSwagger();

View file

@ -0,0 +1,31 @@
using System;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace FilterLists.Directory.Api.Controllers
{
[ApiController]
[ApiExplorerSettings(IgnoreApi = true)]
// TODO: de-duplicate into SharedKernel
public class ErrorController : ControllerBase
{
[Route("/error-local-development")]
public IActionResult ErrorLocalDevelopment([FromServices] IWebHostEnvironment webHostEnvironment)
{
if (webHostEnvironment.EnvironmentName != "Development")
{
throw new InvalidOperationException("This shouldn't be invoked in non-development environments.");
}
var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
return Problem(context.Error.StackTrace, title: context.Error.Message);
}
[Route("/error")]
public IActionResult Error()
{
return Problem();
}
}
}

View file

@ -20,8 +20,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddRouting(o => o.LowercaseUrls = true);
services.AddControllers()
.AddJsonOptions(o => o.JsonSerializerOptions.IgnoreNullValues = true);
services.AddControllers().AddJsonOptions(o => o.JsonSerializerOptions.IgnoreNullValues = true);
services.AddSwaggerGen();
services.AddApplicationServices(Configuration);
}
@ -29,11 +28,7 @@ public void ConfigureServices(IServiceCollection services)
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseApplication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseExceptionHandler(env.IsDevelopment() ? "/error-local-development" : "/error");
app.UseRouting();
app.UseEndpoints(e => e.MapControllers());
app.UseSwagger();