1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

Add error handling to identity accounts controller (#1909)

This commit is contained in:
Matt Gibson 2022-03-14 20:08:01 -05:00 committed by GitHub
parent 76ddcfa2dc
commit 9a9c9d4bf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 1 deletions

View File

@ -8,12 +8,14 @@ using Bit.Core.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Bit.SharedWeb.Utilities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Bit.Identity.Controllers
{
[Route("accounts")]
[ExceptionHandlerFilter]
public class AccountsController : Controller
{
private readonly ILogger<AccountsController> _logger;

View File

@ -58,7 +58,6 @@ namespace Bit.Identity
services.AddMemoryCache();
// Mvc
// MVC
services.AddMvc(config =>
{
config.Filters.Add(new ModelStateValidationFilterAttribute());

View File

@ -0,0 +1,93 @@
using System;
using Bit.Core.Exceptions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Stripe;
using InternalApi = Bit.Core.Models.Api;
namespace Bit.SharedWeb.Utilities
{
public class ExceptionHandlerFilterAttribute : ExceptionFilterAttribute
{
public ExceptionHandlerFilterAttribute()
{
}
public override void OnException(ExceptionContext context)
{
var errorMessage = "An error has occurred.";
var exception = context.Exception;
if (exception == null)
{
// Should never happen.
return;
}
InternalApi.ErrorResponseModel internalErrorModel = null;
if (exception is BadRequestException badRequestException)
{
context.HttpContext.Response.StatusCode = 400;
if (badRequestException.ModelState != null)
{
internalErrorModel = new InternalApi.ErrorResponseModel(badRequestException.ModelState);
}
else
{
errorMessage = badRequestException.Message;
}
}
else if (exception is GatewayException)
{
errorMessage = exception.Message;
context.HttpContext.Response.StatusCode = 400;
}
else if (exception is NotSupportedException && !string.IsNullOrWhiteSpace(exception.Message))
{
errorMessage = exception.Message;
context.HttpContext.Response.StatusCode = 400;
}
else if (exception is ApplicationException)
{
context.HttpContext.Response.StatusCode = 402;
}
else if (exception is NotFoundException)
{
errorMessage = "Resource not found.";
context.HttpContext.Response.StatusCode = 404;
}
else if (exception is SecurityTokenValidationException)
{
errorMessage = "Invalid token.";
context.HttpContext.Response.StatusCode = 403;
}
else if (exception is UnauthorizedAccessException)
{
errorMessage = "Unauthorized.";
context.HttpContext.Response.StatusCode = 401;
}
else
{
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<ExceptionHandlerFilterAttribute>>();
logger.LogError(0, exception, exception.Message);
errorMessage = "An unhandled server error has occurred.";
context.HttpContext.Response.StatusCode = 500;
}
var errorModel = internalErrorModel ?? new InternalApi.ErrorResponseModel(errorMessage);
var env = context.HttpContext.RequestServices.GetRequiredService<IWebHostEnvironment>();
if (env.IsDevelopment())
{
errorModel.ExceptionMessage = exception.Message;
errorModel.ExceptionStackTrace = exception.StackTrace;
errorModel.InnerExceptionMessage = exception?.InnerException?.Message;
}
context.Result = new ObjectResult(errorModel);
}
}
}