When you make a request via the SDK, there is a chance of request failing due to various reasons. When such a failure happens, an exception corresponding to the error occurred is thrown.
BadRequestExceptionIf the request returns with status code400UnauthorizedExceptionIf the request returns with status code401ForbiddenExceptionIf the request returns with status code403NotFoundExceptionIf the request returns with status code404MethodNotAllowedExceptionIf the request returns with status code405TooManyRequestsExceptionIf the request returns with status code429PleaseContactBunqExceptionIf the request returns with status code500. If you get this exception, please contact us preferably via the support chat in the bunq app.UnknownApiErrorExceptionIf none of the above mentioned exceptions are thrown, this exception will be thrown instead.
For more information regarding these errors, please take a look on the documentation page here: https://doc.bunq.com/api/1/page/errors
All the exceptions have the same base exception which looks like this:
public class ApiException extends RuntimeException {
private int responseCode;
public ApiException(String message, int responseCode) {
// Some hidden code
}
public int getResponseCode() {
return responseCode;
}
}This means that each exception will have a response code and an error message related to the specific error returned by API.
Since each API error has a distinct SDK exception type corresponding to it, you can catch the exact exceptions you expect 👏.
import com.bunq.sdk.context.ApiContext;
import com.bunq.sdk.context.ApiEnvironmentType;
import com.bunq.sdk.exception.BadRequestException;
class BadRequest {
private final String API_KEY = "Some invalid key";
private final String DESCRIPTION = "This will throw BadRequestException.";
public void main(){
try{
new ApiContext(ApiEnvironmentType.SANDBOX, this.API_KEY, this.DESCRIPTION);
}catch (BadRequestException error){
System.out.println(error.getMessage());
System.out.println(error.getResponseCode());
}
}
}