Skip to content

Commit fbbd9ad

Browse files
authored
Merge pull request #73 from bunq/#63-add-response-id-to-failed-request
(#63) add response id to failed request
2 parents 45a7690 + 5a2a805 commit fbbd9ad

13 files changed

+126
-28
lines changed

.idea/.idea.BunqSdk/.idea/contentModel.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Bunq.Sdk.Context;
3+
using Bunq.Sdk.Exception;
4+
using Bunq.Sdk.Model.Generated.Endpoint;
5+
using Xunit;
6+
7+
namespace Bunq.Sdk.Tests.Http
8+
{
9+
public class ResponseIdOnBadRequestTest : BunqSdkTestBase
10+
{
11+
/// <summary>
12+
/// API context to use for the test API calls.
13+
/// </summary>
14+
private static readonly ApiContext API_CONTEXT = GetApiContext();
15+
16+
/// <summary>
17+
/// Invalid user id to trigger BadRequestException
18+
/// </summary>
19+
private const int INVALID_USER_PERSON_ID = 0;
20+
21+
[Fact]
22+
public void TestBadRequestWithResponseId()
23+
{
24+
var caughtException = Assert.Throws<BadRequestException>(
25+
() => UserPerson.Get(API_CONTEXT, INVALID_USER_PERSON_ID)
26+
);
27+
28+
Assert.NotNull(caughtException.ResponseId);
29+
}
30+
}
31+
}

BunqSdk/Exception/ApiException.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ namespace Bunq.Sdk.Exception
44
public class ApiException : System.Exception
55
{
66
public int ResponseCode { get; private set; }
7+
public string ResponseId { get; private set; }
78

9+
/// <inheritdoc />
810
/// <param name="responseCode">The HTTP Response code of the failed request.</param>
911
/// <param name="message">The error message related to this exception.</param>
10-
public ApiException(int responseCode, string message) : base(message)
12+
/// <param name="responseId"></param>
13+
protected ApiException(int responseCode, string message, string responseId) : base(message)
1114
{
1215
ResponseCode = responseCode;
16+
ResponseId = responseId;
1317
}
1418
}
1519
}

BunqSdk/Exception/BadRequestException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33
public class BadRequestException : ApiException
44
{
5-
public BadRequestException(int responseCode, string message) : base(responseCode, message)
5+
public BadRequestException(int responseCode, string message, string responseId)
6+
: base(responseCode, message, responseId)
67
{
78
}
89
}
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace 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
}

BunqSdk/Exception/ForbiddenException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33
public class ForbiddenException : ApiException
44
{
5-
public ForbiddenException(int responseCode, string message) : base(responseCode, message)
5+
public ForbiddenException(int responseCode, string message, string responseId)
6+
: base(responseCode, message, responseId)
67
{
78
}
89
}

BunqSdk/Exception/MethodNotAllowedException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33
public class MethodNotAllowedException : ApiException
44
{
5-
public MethodNotAllowedException(int responseCode, string message) : base(responseCode, message)
5+
public MethodNotAllowedException(int responseCode, string message, string responseId)
6+
: base(responseCode, message, responseId)
67
{
78
}
89
}

BunqSdk/Exception/NotFoundException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33
public class NotFoundException : ApiException
44
{
5-
public NotFoundException(int responseCode, string message) : base(responseCode, message)
5+
public NotFoundException(int responseCode, string message, string responseId)
6+
: base(responseCode, message, responseId)
67
{
78
}
89
}

BunqSdk/Exception/PleaseContactBunqException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33
public class PleaseContactBunqException : ApiException
44
{
5-
public PleaseContactBunqException(int responseCode, string message) : base(responseCode, message)
5+
public PleaseContactBunqException(int responseCode, string message, string responseId)
6+
: base(responseCode, message, responseId)
67
{
78
}
89
}

BunqSdk/Exception/TooManyRequestsException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{
33
public class TooManyRequestsException : ApiException
44
{
5-
public TooManyRequestsException(int responseCode, string message) : base(responseCode, message)
5+
public TooManyRequestsException(int responseCode, string message, string responseId)
6+
: base(responseCode, message, responseId)
67
{
78
}
89
}

0 commit comments

Comments
 (0)