Exceptions
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 raised.
Possible Exceptions
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 raised, this exception will be raised instead.
For more information regarding these errors, please take a look on the documentation page here:
ErrorsBase exception
All the exceptions have the same base exception which looks like this:
class ApiException(Exception):
def __init__(self,
message: str,
response_code: int) -> None:
pass
@property
def message(self) -> str:
return self._message
@property
def response_code(self) -> int:
return self._response_codeThis means that each exception will have the response code and the error message related to the specific exception that has been raised.
Exception handling
Because we raise different exceptions for each error, you can catch an error if you expect it to be raised.
from bunq.sdk.exception.bad_request_exception import BadRequestException
from bunq.sdk.context.api_context import ApiEnvironmentType, ApiContext
API_KEY = "Some invalid API key"
DESCRIPTION = "This will raise a BadRequestException"
try:
# Make a call that might raise an exception
ApiContext.create(ApiEnvironmentType.SANDBOX, API_KEY, DESCRIPTION)
except BadRequestException as error:
# Do something if exception is raised
print(error.response_code)
print(error.message) # or just print(error)Last updated
Was this helpful?