Skip to content

Commit bfd9663

Browse files
committed
Better exception and docs for missing type info, fixes JohnnyCrazy#472
1 parent 27773ca commit bfd9663

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎SpotifyAPI.Docs/docs/iplayableitem.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ foreach (PlaylistTrack<IPlayableItem> item in playlist.Tracks.Items)
3737
```
3838

3939
To this day, `IPlayableItem` can only be `FullTrack` or `FullEpisode`.
40+
41+
## Fields
42+
43+
When requesting just a subset of fields using the `fields` query parameter, the call might fail with an exception similar to `Received unkown playlist element type`. For example, the following call fails:
44+
45+
```csharp
46+
var playlistGetItemsRequest = new PlaylistGetItemsRequest();
47+
playlistGetItemsRequest.Fields.Add("items(track(name))");
48+
var playlistItems = await spotify.Playlists.GetItems("YourPlaylistId", playlistGetItemsRequest);
49+
```
50+
51+
By requesting just the track name from the items, we don't have any kind of type information of the item itself. Thus, we're unable to cast it to the correct model. To fix this, include the type in the fields as well:
52+
53+
```csharp
54+
playlistGetItemsRequest.Fields.Add("items(track(name,type))");
55+
```
56+

‎SpotifyAPI.Web/Models/Converters/PlayableItemConverter.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public class PlayableItemConverter : JsonConverter
3636
}
3737
else
3838
{
39-
throw new Exception($"Received unkown playlist element type: {type}");
39+
throw new Exception($@"Received unkown playlist element type: {type}.
40+
If you're requesting a subset of available fields via the fields query paramter,
41+
make sure to include at least the type field. Often it's `items(track(type))` or `item(type)`");
4042
}
4143
}
4244

0 commit comments

Comments
 (0)