Skip to content

Commit c2e79d5

Browse files
author
Kevin Hellemun
committed
Base excaption handler done.
1 parent 9d3e502 commit c2e79d5

13 files changed

+170
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,4 @@ bunq.conf
265265
**/Tmp/
266266
bunq-test.conf
267267
config.json
268+
.idea/.idea.BunqSdk/.idea/preferred-vcs.xml

BunqSdk/BunqSdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
<Content Include="../README.md" />
3535
<Content Include="../.gitignore" />
3636
</ItemGroup>
37-
</Project>
37+
</Project>

BunqSdk/Exception/ApiException.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,10 @@ namespace Bunq.Sdk.Exception
55
/// <summary>
66
/// Exception triggered by API requests failed on the server side.
77
/// </summary>
8-
public class ApiException : System.Exception
8+
public class ApiException : BunqError
99
{
10-
/// <summary>
11-
/// Glue to concatenate the error messages.
12-
/// </summary>
13-
private const string GLUE_ERROR_MESSAGES = "\n";
14-
15-
public int ResponseCode { get; private set; }
16-
public IList<string> Messages { get; private set; }
17-
18-
/// <param name="responseCode">The HTTP Response code of the failed request.</param>
19-
/// <param name="messages">The list of messages related to this exception.</param>
20-
public ApiException(int responseCode, IList<string> messages) : base(
21-
ConcatenateMessages(messages))
22-
{
23-
ResponseCode = responseCode;
24-
Messages = messages;
25-
}
26-
27-
private static string ConcatenateMessages(IEnumerable<string> messages)
10+
public ApiException(int responseCode, string messages) : base(responseCode, messages)
2811
{
29-
return string.Join(GLUE_ERROR_MESSAGES, messages);
3012
}
3113
}
3214
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Bunq.Sdk.Exception
4+
{
5+
public class BadRequestException : BunqError
6+
{
7+
public BadRequestException(int responseCode, string messages) : base(responseCode, messages)
8+
{
9+
}
10+
}
11+
}

BunqSdk/Exception/BunqError.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
3+
namespace Bunq.Sdk.Exception
4+
{
5+
public class BunqError : System.Exception
6+
{
7+
public int ResponseCode { get; private set; }
8+
public string Messages { get; private set; }
9+
10+
/// <param name="responseCode">The HTTP Response code of the failed request.</param>
11+
/// <param name="messages">The list of messages related to this exception.</param>
12+
public BunqError(int responseCode, string messages) : base(messages)
13+
{
14+
ResponseCode = responseCode;
15+
Messages = messages;
16+
}
17+
18+
protected BunqError()
19+
{
20+
throw new System.NotImplementedException();
21+
}
22+
}
23+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Bunq.Sdk.Exception
4+
{
5+
public class ForbiddenException : BunqError
6+
{
7+
public ForbiddenException(int responseCode, string messages) : base(responseCode, messages)
8+
{
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Bunq.Sdk.Exception
4+
{
5+
public class MethodNotAllowedException : BunqError
6+
{
7+
public MethodNotAllowedException(int responseCode, string messages) : base(responseCode, messages)
8+
{
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Bunq.Sdk.Exception
4+
{
5+
public class NotFoundException : BunqError
6+
{
7+
public NotFoundException(int responseCode, string messages) : base(responseCode, messages)
8+
{
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Bunq.Sdk.Exception
4+
{
5+
public class PleaseContactBunqException : BunqError
6+
{
7+
public PleaseContactBunqException(int responseCode, string messages) : base(responseCode, messages)
8+
{
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)