mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(apis): ✨ supply basic/consistent error response
This commit is contained in:
parent
006a066cd1
commit
5fad42ba1f
4 changed files with 66 additions and 14 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue