1+ using System . Collections . Generic ;
2+ using Bunq . Sdk . Model ;
3+
4+ namespace Bunq . Sdk . Exception
5+ {
6+ public class ExceptionHandler
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 BunqError CreateExceptionForResponse ( int responseCode , IList < string > messages )
23+ {
24+ var errorMessage = ConcatenateMessages ( messages ) ;
25+
26+ if ( responseCode == HTTP_RESPONSE_CODE_BAD_REQUEST )
27+ {
28+ return new BadRequestException ( responseCode , errorMessage ) ;
29+ }
30+ if ( responseCode == HTTP_RESPONSE_CODE_UNAUTHORIZED )
31+ {
32+ return new UnauthorizedException ( responseCode , errorMessage ) ;
33+ }
34+ if ( responseCode == HTTP_RESPONSE_CODE_FORBIDDEN )
35+ {
36+ return new ForbiddenException ( responseCode , errorMessage ) ;
37+ }
38+ if ( responseCode == HTTP_RESPONSE_CODE_NOT_FOUND )
39+ {
40+ return new NotFoundException ( responseCode , errorMessage ) ;
41+ }
42+ if ( responseCode == HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED )
43+ {
44+ return new MethodNotAllowedException ( responseCode , errorMessage ) ;
45+ }
46+ if ( responseCode == HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS )
47+ {
48+ return new ToManyRequestsException ( responseCode , errorMessage ) ;
49+ }
50+ if ( responseCode == HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR )
51+ {
52+ return new PleaseContactBunqException ( responseCode , errorMessage ) ;
53+ }
54+
55+ return new ApiException ( responseCode , errorMessage ) ;
56+ }
57+
58+ private static string ConcatenateMessages ( IEnumerable < string > messages )
59+ {
60+ return string . Join ( GLUE_ERROR_MESSAGES , messages ) ;
61+ }
62+ }
63+ }
0 commit comments