Skip to content

Commit ac850ca

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

13 files changed

+92
-103
lines changed

BunqSdk/Exception/ApiException.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
/// <summary>
6-
/// Exception triggered by API requests failed on the server side.
7-
/// </summary>
8-
public class ApiException : BunqError
5+
public class ApiException : System.Exception
96
{
10-
public ApiException(int responseCode, string messages) : base(responseCode, messages)
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 ApiException(int responseCode, string messages) : base(messages)
13+
{
14+
ResponseCode = responseCode;
15+
Messages = messages;
16+
}
17+
18+
protected ApiException()
1119
{
20+
throw new System.NotImplementedException();
1221
}
13-
}
14-
}
22+
}
23+
}

BunqSdk/Exception/BadRequestException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
public class BadRequestException : BunqError
5+
public class BadRequestException : ApiException
66
{
77
public BadRequestException(int responseCode, string messages) : base(responseCode, messages)
88
{

BunqSdk/Exception/BunqError.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

BunqSdk/Exception/ExceptionHandler.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

BunqSdk/Exception/ForbiddenException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
public class ForbiddenException : BunqError
5+
public class ForbiddenException : ApiException
66
{
77
public ForbiddenException(int responseCode, string messages) : base(responseCode, messages)
88
{

BunqSdk/Exception/MethodNotAllowedException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
public class MethodNotAllowedException : BunqError
5+
public class MethodNotAllowedException : ApiException
66
{
77
public MethodNotAllowedException(int responseCode, string messages) : base(responseCode, messages)
88
{

BunqSdk/Exception/NotFoundException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
public class NotFoundException : BunqError
5+
public class NotFoundException : ApiException
66
{
77
public NotFoundException(int responseCode, string messages) : base(responseCode, messages)
88
{

BunqSdk/Exception/PleaseContactBunqException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
public class PleaseContactBunqException : BunqError
5+
public class PleaseContactBunqException : ApiException
66
{
77
public PleaseContactBunqException(int responseCode, string messages) : base(responseCode, messages)
88
{

BunqSdk/Exception/ToManyRequestsException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bunq.Sdk.Exception
44
{
5-
public class ToManyRequestsException : BunqError
5+
public class ToManyRequestsException : ApiException
66
{
77
public ToManyRequestsException(int responseCode, string messages) : base(responseCode, messages)
88
{

0 commit comments

Comments
 (0)