Spotify.NET/SpotifyAPI.Docs/docs/error_handling.md

47 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2020-06-03 16:44:13 +01:00
---
id: error_handling
title: Error Handling
---
API calls can fail when input data is malformed or the server detects issues with the request. As an example, the following request obviously fails:
```csharp
var track = await spotify.Tracks.Get("NotExistingTrackId");
Console.WriteLine(track.Name);
```
When a request fails, an `APIException` is thrown. Specific errors may throw a child exception of `APIException`.
2020-06-03 16:44:13 +01:00
## APIException
A very general API error. The message is parsed from the API response's JSON body and the response is available as a public property.
2020-06-03 16:44:13 +01:00
```csharp
try {
var track = await spotify.Tracks.Get("NotExistingTrackId");
} catch(APIException e) {
// Prints: invalid id
Console.WriteLine(e.Message);
// Prints: BadRequest
Console.WriteLine(e.Response?.StatusCode);
}
```
## APIUnauthorizedException
Provides the same properties as `APIException` and occurs when the access token is expired or not provided. Notice that an access token has to be included in **every** request. Spotify does not allow unauthorized API access.
2020-06-03 16:44:13 +01:00
## APITooManyRequestsException
Provides the same properties as `APIException` and occurs when too many requests has been sent by your application. It also provides the property `TimeSpan RetryAfter`, which maps to the received `Retry-After` header.
2020-06-03 16:44:13 +01:00
```csharp
try {
// call it very often?
var track = await spotify.Tracks.Get("1s6ux0lNiTziSrd7iUAADH");
} catch(APITooManyRequestsException e) {
// Prints: seconds to wait, often 1 or 2
Console.WriteLine(e.RetryAfter);
}
```