Skip to content

Commit

Permalink
API.Response: Prevent null reference exceptions
Browse files Browse the repository at this point in the history
When received response is invalid (status code is not success), its
content/headers don't have to be set. Code now properly checks for that
scenario.

This prevents occasional "Object reference not set to an object" message
when API.Spotify.GetPlaybackState() is called.
  • Loading branch information
lookeypl committed Apr 12, 2024
1 parent ae23236 commit 256f09b
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion LukeBot.API/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ internal ResponseData(HttpResponseMessage msg)
// if it is a JSON response, it might have error/message fields available
if (!msg.IsSuccessStatusCode)
{
if (msg.Content.Headers.ContentType.MediaType == "application/json")
if (msg.Content != null && msg.Content.Headers != null &&
msg.Content.Headers.ContentType.MediaType == "application/json")
{
Task<string> respStringTask = msg.Content.ReadAsStringAsync();
respStringTask.Wait();
Expand Down

0 comments on commit 256f09b

Please sign in to comment.