1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23
34namespace Bunq . Sdk . Exception
45{
6+ /// <summary>
7+ /// This class makes sure that the correct exception is thrown for the given response code.
8+ /// </summary>
59 public class ExceptionFactory
610 {
711 /// <summary>
@@ -16,39 +20,49 @@ public class ExceptionFactory
1620 private const int HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR = 500 ;
1721
1822 /// <summary>
19- /// Glue to concatenate the error messages .
23+ /// String format constants .
2024 /// </summary>
21- private const string GLUE_ERROR_MESSAGES = "\n " ;
25+ private const string FORMAT_ERROR_MESSAGE = "Response id to help bunq debug: {0}. \n Error message: {1} " ;
2226
2327 /// <returns>The exception that belongs to this status code.</returns>
24- public static ApiException CreateExceptionForResponse ( int responseCode , IList < string > messages )
28+ public static ApiException CreateExceptionForResponse (
29+ int responseCode ,
30+ IEnumerable < string > messages ,
31+ string responseId
32+ )
2533 {
26- var errorMessage = ConcatenateMessages ( messages ) ;
34+ var errorMessage = FormatExceptionMessage ( messages , responseId ) ;
2735
2836 switch ( responseCode )
2937 {
3038 case HTTP_RESPONSE_CODE_BAD_REQUEST :
31- return new BadRequestException ( responseCode , errorMessage ) ;
39+ return new BadRequestException ( responseCode , errorMessage , responseId ) ;
3240 case HTTP_RESPONSE_CODE_UNAUTHORIZED :
33- return new UnauthorizedException ( responseCode , errorMessage ) ;
41+ return new UnauthorizedException ( responseCode , errorMessage , responseId ) ;
3442 case HTTP_RESPONSE_CODE_FORBIDDEN :
35- return new ForbiddenException ( responseCode , errorMessage ) ;
43+ return new ForbiddenException ( responseCode , errorMessage , responseId ) ;
3644 case HTTP_RESPONSE_CODE_NOT_FOUND :
37- return new NotFoundException ( responseCode , errorMessage ) ;
45+ return new NotFoundException ( responseCode , errorMessage , responseId ) ;
3846 case HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED :
39- return new MethodNotAllowedException ( responseCode , errorMessage ) ;
47+ return new MethodNotAllowedException ( responseCode , errorMessage , responseId ) ;
4048 case HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS :
41- return new TooManyRequestsException ( responseCode , errorMessage ) ;
49+ return new TooManyRequestsException ( responseCode , errorMessage , responseId ) ;
4250 case HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR :
43- return new PleaseContactBunqException ( responseCode , errorMessage ) ;
51+ return new PleaseContactBunqException ( responseCode , errorMessage , responseId ) ;
4452 default :
45- return new UnknownApiErrorException ( responseCode , errorMessage ) ;
53+ return new UnknownApiErrorException ( responseCode , errorMessage , responseId ) ;
4654 }
4755 }
4856
49- private static string ConcatenateMessages ( IEnumerable < string > messages )
57+ /// <summary>
58+ /// Formats the exception message accordingly.
59+ /// </summary>
60+ private static string FormatExceptionMessage ( IEnumerable < string > messages , string responseId )
5061 {
51- return string . Join ( GLUE_ERROR_MESSAGES , messages ) ;
62+ return string . Format ( FORMAT_ERROR_MESSAGE ,
63+ responseId ,
64+ string . Join ( Environment . NewLine , messages )
65+ ) ;
5266 }
5367 }
5468}
0 commit comments