1+ using System . Collections . Generic ;
2+ using Bunq . Sdk . Model ;
3+
4+ namespace Bunq . Sdk . Exception
5+ {
6+ public class ExceptionFactory
7+ {
8+ private const int HTTP_RESPONSE_CODE_BAD_REQUEST = 400 ;
9+ private const int HTTP_RESPONSE_CODE_UNAUTHORIZED = 401 ;
10+ private const int HTTP_RESPONSE_CODE_FORBIDDEN = 403 ;
11+ private const int HTTP_RESPONSE_CODE_NOT_FOUND = 404 ;
12+ private const int HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED = 405 ;
13+ private const int HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS = 429 ;
14+ private const int HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR = 500 ;
15+
16+ /// <summary>
17+ /// Glue to concatenate the error messages.
18+ /// </summary>
19+ private const string GLUE_ERROR_MESSAGES = "\n " ;
20+
21+
22+ public static ApiException CreateExceptionForResponse ( int responseCode , IList < string > messages )
23+ {
24+ var errorMessage = ConcatenateMessages ( messages ) ;
25+
26+ switch ( responseCode )
27+ {
28+ case HTTP_RESPONSE_CODE_BAD_REQUEST :
29+ return new BadRequestException ( responseCode , errorMessage ) ;
30+ case HTTP_RESPONSE_CODE_UNAUTHORIZED :
31+ return new UnauthorizedException ( responseCode , errorMessage ) ;
32+ case HTTP_RESPONSE_CODE_FORBIDDEN :
33+ return new ForbiddenException ( responseCode , errorMessage ) ;
34+ case HTTP_RESPONSE_CODE_NOT_FOUND :
35+ return new NotFoundException ( responseCode , errorMessage ) ;
36+ case HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED :
37+ return new MethodNotAllowedException ( responseCode , errorMessage ) ;
38+ case HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS :
39+ return new ToManyRequestsException ( responseCode , errorMessage ) ;
40+ case HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR :
41+ return new PleaseContactBunqException ( responseCode , errorMessage ) ;
42+ default :
43+ return new UnknownApiErrorException ( responseCode , errorMessage ) ;
44+ }
45+ }
46+
47+ private static string ConcatenateMessages ( IEnumerable < string > messages )
48+ {
49+ return string . Join ( GLUE_ERROR_MESSAGES , messages ) ;
50+ }
51+ }
52+ }
0 commit comments