Skip to content

Commit c98f79a

Browse files
authored
Merge pull request #18 from bunq/feature/exception-handler
Feature/exception handler
2 parents 2c42ef7 + dfc5ae8 commit c98f79a

16 files changed

+235
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ bunq.conf
7171
context-save-restore-test.conf
7272
/tmp
7373
config.properties
74+
bunq-test.conf

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,7 @@ API key before running the example!
172172
## Running tests
173173
Information regarding the test cases can be found in the [README.md](./src/test/README.md)
174174
located in [test](/src/test).
175+
176+
## Exceptions
177+
The SDK can throw multiple exceptions. For an overview of these exceptions please
178+
take a look at [EXCEPTIONS.md](./src/main/java/com/bunq/sdk/exception/EXCEPTIONS.md)
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
package com.bunq.sdk.exception;
22

3-
import java.util.List;
4-
5-
/**
6-
* Exception triggered by API requests failed on the server side.
7-
*/
83
public class ApiException extends RuntimeException {
94

10-
private static final String GLUE_ERROR_MESSAGES = "\n";
11-
125
private int responseCode;
13-
private List<String> messages;
146

15-
/**
16-
* @param responseCode The HTTP Response code of the failed request.
17-
* @param messages The list of messages related to this exception.
18-
*/
19-
public ApiException(int responseCode, List<String> messages) {
20-
super(concatenateMessages(messages));
21-
this.responseCode = responseCode;
22-
this.messages = messages;
23-
}
7+
public ApiException(String message, int responseCode) {
8+
super(message);
249

25-
private static String concatenateMessages(List<String> messages) {
26-
return String.join(GLUE_ERROR_MESSAGES, messages);
10+
this.responseCode = responseCode;
2711
}
2812

2913
public int getResponseCode() {
3014
return responseCode;
3115
}
3216

33-
public List<String> getMessages() {
34-
return messages;
35-
}
36-
3717
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bunq.sdk.exception;
2+
3+
public class BadRequestException extends ApiException {
4+
5+
public BadRequestException(String message, int responseCode) {
6+
super(message, responseCode);
7+
}
8+
9+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Exceptions
2+
When you make a request via the SDK, there is a chance of request failing
3+
due to various reasons. When such a failure happens, an exception
4+
corresponding to the error occurred is thrown.
5+
6+
7+
----
8+
#### Possible Exceptions
9+
* `BadRequestException` If the request returns with status code `400`
10+
* `UnauthorizedException` If the request returns with status code `401`
11+
* `ForbiddenException` If the request returns with status code `403`
12+
* `NotFoundException` If the request returns with status code `404`
13+
* `MethodNotAllowedException` If the request returns with status code `405`
14+
* `TooManyRequestsException` If the request returns with status code `429`
15+
* `PleaseContactBunqException` If the request returns with status code `500`.
16+
If you get this exception, please contact us preferably via the support chat in the bunq app.
17+
* `UnknownApiErrorException` If none of the above mentioned exceptions are thrown,
18+
this exception will be thrown instead.
19+
20+
For more information regarding these errors, please take a look on the documentation
21+
page here: https://doc.bunq.com/api/1/page/errors
22+
23+
---
24+
#### Base exception
25+
All the exceptions have the same base exception which looks like this:
26+
```java
27+
public class ApiException extends RuntimeException {
28+
29+
private int responseCode;
30+
31+
public ApiException(String message, int responseCode) {
32+
// Some hidden code
33+
}
34+
35+
public int getResponseCode() {
36+
return responseCode;
37+
}
38+
39+
}
40+
```
41+
This means that each exception will have a response code and an error message
42+
related to the specific error returned by API.
43+
44+
---
45+
#### Exception handling
46+
Since each API error has a distinct SDK exception type corresponding to it,
47+
you can catch the exact exceptions you expect 👏.
48+
49+
```java
50+
import com.bunq.sdk.context.ApiContext;
51+
import com.bunq.sdk.context.ApiEnvironmentType;
52+
import com.bunq.sdk.exception.BadRequestException;
53+
54+
class BadRequest {
55+
private final String API_KEY = "Some invalid key";
56+
private final String DESCRIPTION = "This will throw BadRequestException.";
57+
58+
public void main(){
59+
try{
60+
new ApiContext(ApiEnvironmentType.SANDBOX, this.API_KEY, this.DESCRIPTION);
61+
}catch (BadRequestException error){
62+
System.out.println(error.getMessage());
63+
System.out.println(error.getResponseCode());
64+
}
65+
}
66+
67+
}
68+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.bunq.sdk.exception;
2+
3+
import java.util.List;
4+
5+
public class ExceptionFactory {
6+
7+
/**
8+
* HTTP error response codes constants.
9+
*/
10+
private static final int HTTP_RESPONSE_CODE_BAD_REQUEST = 400;
11+
private static final int HTTP_RESPONSE_CODE_UNAUTHORIZED = 401;
12+
private static final int HTTP_RESPONSE_CODE_FORBIDDEN = 403;
13+
private static final int HTTP_RESPONSE_CODE_NOT_FOUND = 404;
14+
private static final int HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED = 405;
15+
private static final int HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS = 429;
16+
private static final int HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR = 500;
17+
18+
/**
19+
* Some glue to glue the error message together.
20+
*/
21+
private static final String GLUE_ERROR_MESSAGES = "\n";
22+
23+
/**
24+
*
25+
* @param responseCode The HTTP response code related to the request.
26+
* @param messages The error message related to the exception that should be thrown.
27+
*
28+
* @return The exception that belongs to this status code.
29+
*/
30+
public static ApiException createExceptionForResponse(int responseCode, List<String> messages){
31+
String error_message = concatenateMessages(messages);
32+
33+
switch (responseCode) {
34+
case HTTP_RESPONSE_CODE_BAD_REQUEST:
35+
return new BadRequestException(error_message, responseCode);
36+
case HTTP_RESPONSE_CODE_UNAUTHORIZED:
37+
return new UnauthorizedException(error_message, responseCode);
38+
case HTTP_RESPONSE_CODE_FORBIDDEN:
39+
return new UnauthorizedException(error_message, responseCode);
40+
case HTTP_RESPONSE_CODE_NOT_FOUND:
41+
return new NotFoundException(error_message, responseCode);
42+
case HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED:
43+
return new MethodNotAllowedException(error_message, responseCode);
44+
case HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS:
45+
return new TooManyRequestsException(error_message, responseCode);
46+
case HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR:
47+
return new PleaseContactBunqException(error_message, responseCode);
48+
default:
49+
return new UnknownApiErrorException(error_message, responseCode);
50+
}
51+
}
52+
53+
private static String concatenateMessages(List<String> messages) {
54+
return String.join(GLUE_ERROR_MESSAGES, messages);
55+
}
56+
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bunq.sdk.exception;
2+
3+
public class ForbiddenException extends ApiException {
4+
5+
public ForbiddenException(String message, int responseCode) {
6+
super(message, responseCode);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bunq.sdk.exception;
2+
3+
public class MethodNotAllowedException extends ApiException {
4+
5+
public MethodNotAllowedException(String message, int responseCode) {
6+
super(message, responseCode);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bunq.sdk.exception;
2+
3+
public class NotFoundException extends ApiException {
4+
5+
public NotFoundException(String message, int responseCode) {
6+
super(message, responseCode);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.bunq.sdk.exception;
2+
3+
public class PleaseContactBunqException extends ApiException {
4+
5+
public PleaseContactBunqException(String message, int responseCode) {
6+
super(message, responseCode);
7+
}
8+
9+
}

0 commit comments

Comments
 (0)