mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[AC-2888] Improve consolidated billing error handling (#4548)
* Fix error handling in provider setup process This update ensures that when 'enable-consolidated-billing' is on, any exception thrown during the Stripe customer or subscription setup process for the provider will block the remainder of the setup process so the provider does not enter an invalid state * Refactor the way BillingException is thrown Made it simpler to just use the exception constructor and also ensured it was added to the exception handling middleware so it could provide a simple response to the client * Handle all Stripe exceptions in exception handling middleware * Fixed error response output for billing's provider controllers * Cleaned up billing owned provider controllers Changes were made based on feature updates by product and stuff that's no longer needed. No need to expose sensitive endpoints when they're not being used. * Reafctored get invoices Removed unnecssarily bloated method from SubscriberService * Updated error handling for generating the client invoice report * Moved get provider subscription to controller This is only used once and the service layer doesn't seem like the correct choice anymore when thinking about error handling with retrieval * Handled bad request for update tax information * Split out Stripe configuration from unauthorization * Run dotnet format * Addison's feedback
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
using Bit.Api.Models.Public.Response;
|
||||
using System.Text;
|
||||
using Bit.Api.Models.Public.Response;
|
||||
using Bit.Core.Billing;
|
||||
using Bit.Core.Exceptions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
@ -49,18 +51,18 @@ public class ExceptionHandlerFilterAttribute : ExceptionFilterAttribute
|
||||
errorMessage = badRequestException.Message;
|
||||
}
|
||||
}
|
||||
else if (exception is StripeException stripeException && stripeException?.StripeError?.Type == "card_error")
|
||||
else if (exception is StripeException { StripeError.Type: "card_error" } stripeCardErrorException)
|
||||
{
|
||||
context.HttpContext.Response.StatusCode = 400;
|
||||
if (_publicApi)
|
||||
{
|
||||
publicErrorModel = new ErrorResponseModel(stripeException.StripeError.Param,
|
||||
stripeException.Message);
|
||||
publicErrorModel = new ErrorResponseModel(stripeCardErrorException.StripeError.Param,
|
||||
stripeCardErrorException.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
internalErrorModel = new InternalApi.ErrorResponseModel(stripeException.StripeError.Param,
|
||||
stripeException.Message);
|
||||
internalErrorModel = new InternalApi.ErrorResponseModel(stripeCardErrorException.StripeError.Param,
|
||||
stripeCardErrorException.Message);
|
||||
}
|
||||
}
|
||||
else if (exception is GatewayException)
|
||||
@ -68,6 +70,40 @@ public class ExceptionHandlerFilterAttribute : ExceptionFilterAttribute
|
||||
errorMessage = exception.Message;
|
||||
context.HttpContext.Response.StatusCode = 400;
|
||||
}
|
||||
else if (exception is BillingException billingException)
|
||||
{
|
||||
errorMessage = billingException.Response;
|
||||
context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
}
|
||||
else if (exception is StripeException stripeException)
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<ExceptionHandlerFilterAttribute>>();
|
||||
|
||||
var error = stripeException.Message;
|
||||
|
||||
if (stripeException.StripeError != null)
|
||||
{
|
||||
var stringBuilder = new StringBuilder();
|
||||
|
||||
if (!string.IsNullOrEmpty(stripeException.StripeError.Code))
|
||||
{
|
||||
stringBuilder.Append($"{stripeException.StripeError.Code} | ");
|
||||
}
|
||||
|
||||
stringBuilder.Append(stripeException.StripeError.Message);
|
||||
|
||||
if (!string.IsNullOrEmpty(stripeException.StripeError.DocUrl))
|
||||
{
|
||||
stringBuilder.Append($" > {stripeException.StripeError.DocUrl}");
|
||||
}
|
||||
|
||||
error = stringBuilder.ToString();
|
||||
}
|
||||
|
||||
logger.LogError("An unhandled error occurred while communicating with Stripe: {Error}", error);
|
||||
errorMessage = "Something went wrong with your request. Please contact support.";
|
||||
context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
}
|
||||
else if (exception is NotSupportedException && !string.IsNullOrWhiteSpace(exception.Message))
|
||||
{
|
||||
errorMessage = exception.Message;
|
||||
|
Reference in New Issue
Block a user