|
| 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 | +```c# |
| 27 | + public class ApiException : System.Exception |
| 28 | + { |
| 29 | + public int ResponseCode { get;} |
| 30 | + |
| 31 | + /// <param name="responseCode">The HTTP Response code of the failed request.</param> |
| 32 | + /// <param name="message">The error messages related to this exception.</param> |
| 33 | + public ApiException(int responseCode, string message) : base(message) |
| 34 | + { |
| 35 | + // hidden code |
| 36 | + } |
| 37 | + } |
| 38 | +``` |
| 39 | +This means that each exception will have a response code and an error message |
| 40 | +related to the specific error returned by API. |
| 41 | + |
| 42 | +--- |
| 43 | +#### Exception handling |
| 44 | +Since each API error has a distinct SDK exception type corresponding to it, |
| 45 | +you can catch the exact exceptions you expect 👏. |
| 46 | + |
| 47 | +```c# |
| 48 | +using Bunq.Sdk.Context; |
| 49 | +using Bunq.Sdk.Exception; |
| 50 | + |
| 51 | +public class BadRequest |
| 52 | +{ |
| 53 | + private const string API_KEY = "Some invalid API key" |
| 54 | + private const string DESCRIPTION = "This will throw BadRequestException." |
| 55 | + |
| 56 | + public void Run() |
| 57 | + { |
| 58 | + try |
| 59 | + { |
| 60 | + ApiContext.Create(ApiEnvironmentType.SANDBOX, API_KEY, DEVICE_DESCRIPTION); |
| 61 | + } |
| 62 | + catch(BadRequestException error) |
| 63 | + { |
| 64 | + Console.WriteLine(error.getMessage()) |
| 65 | + Console.WriteLine(error.getResponseCode()) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
0 commit comments