1- using Bit . Api . Models . Public . Response ;
1+ using System . Reflection ;
2+ using Bit . Api . Models . Public . Response ;
3+ using Bit . Core ;
24using Bit . Core . Exceptions ;
5+ using Bit . Core . Resources ;
36using Microsoft . AspNetCore . Mvc ;
47using Microsoft . AspNetCore . Mvc . Filters ;
8+ using Microsoft . Extensions . Localization ;
59using Microsoft . IdentityModel . Tokens ;
610using Stripe ;
711using InternalApi = Bit . Core . Models . Api ;
@@ -19,7 +23,7 @@ public ExceptionHandlerFilterAttribute(bool publicApi)
1923
2024 public override void OnException ( ExceptionContext context )
2125 {
22- var errorMessage = "An error has occurred." ;
26+ var errorMessage = GetFormattedMessageFromErrorCode ( context ) ;
2327
2428 var exception = context . Exception ;
2529 if ( exception == null )
@@ -44,10 +48,6 @@ public override void OnException(ExceptionContext context)
4448 internalErrorModel = new InternalApi . ErrorResponseModel ( badRequestException . ModelState ) ;
4549 }
4650 }
47- else
48- {
49- errorMessage = badRequestException . Message ;
50- }
5151 }
5252 else if ( exception is StripeException stripeException && stripeException ? . StripeError ? . Type == "card_error" )
5353 {
@@ -65,12 +65,10 @@ public override void OnException(ExceptionContext context)
6565 }
6666 else if ( exception is GatewayException )
6767 {
68- errorMessage = exception . Message ;
6968 context . HttpContext . Response . StatusCode = 400 ;
7069 }
7170 else if ( exception is NotSupportedException && ! string . IsNullOrWhiteSpace ( exception . Message ) )
7271 {
73- errorMessage = exception . Message ;
7472 context . HttpContext . Response . StatusCode = 400 ;
7573 }
7674 else if ( exception is ApplicationException )
@@ -79,17 +77,17 @@ public override void OnException(ExceptionContext context)
7977 }
8078 else if ( exception is NotFoundException )
8179 {
82- errorMessage = "Resource not found." ;
80+ errorMessage = GetFormattedMessageFromErrorCode ( context , ErrorCodes . ResourceNotFound ) ;
8381 context . HttpContext . Response . StatusCode = 404 ;
8482 }
8583 else if ( exception is SecurityTokenValidationException )
8684 {
87- errorMessage = "Invalid token." ;
85+ errorMessage = GetFormattedMessageFromErrorCode ( context , ErrorCodes . InvalidToken ) ;
8886 context . HttpContext . Response . StatusCode = 403 ;
8987 }
9088 else if ( exception is UnauthorizedAccessException )
9189 {
92- errorMessage = "Unauthorized." ;
90+ errorMessage = GetFormattedMessageFromErrorCode ( context , ErrorCodes . Unauthorized ) ;
9391 context . HttpContext . Response . StatusCode = 401 ;
9492 }
9593 else if ( exception is ConflictException )
@@ -114,7 +112,7 @@ public override void OnException(ExceptionContext context)
114112 {
115113 var logger = context . HttpContext . RequestServices . GetRequiredService < ILogger < ExceptionHandlerFilterAttribute > > ( ) ;
116114 logger . LogError ( 0 , exception , exception . Message ) ;
117- errorMessage = "An unhandled server error has occurred." ;
115+ errorMessage = GetFormattedMessageFromErrorCode ( context , ErrorCodes . UnhandledError ) ;
118116 context . HttpContext . Response . StatusCode = 500 ;
119117 }
120118
@@ -136,4 +134,23 @@ public override void OnException(ExceptionContext context)
136134 context . Result = new ObjectResult ( errorModel ) ;
137135 }
138136 }
137+
138+ private string GetFormattedMessageFromErrorCode ( ExceptionContext context , string alternativeErrorCode = null )
139+ {
140+ var localizerFactory = context . HttpContext . RequestServices . GetRequiredService < IStringLocalizerFactory > ( ) ;
141+
142+ var assemblyName = new AssemblyName ( typeof ( SharedResources ) . GetTypeInfo ( ) . Assembly . FullName ) ;
143+ var localizer = localizerFactory . Create ( "ErrorMessages" , assemblyName . Name ) ;
144+
145+ var errorCode = alternativeErrorCode ?? context . Exception . Message ;
146+ var errorMessage = localizer [ errorCode ] ;
147+
148+ // Get localized error message for the exception message
149+ if ( errorMessage . ResourceNotFound is false )
150+ {
151+ return $ "({ errorCode } ) { localizer [ errorCode ] } ";
152+ }
153+
154+ return context . Exception . Message ;
155+ }
139156}
0 commit comments