Skip to content

Commit

Permalink
Add null checks for json error parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear committed Dec 17, 2023
1 parent 02a4617 commit 756a209
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ShopifySharp/Services/Partner/PartnerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,28 @@ public virtual async Task<JToken> PostAsync(string body, CancellationToken cance
/// </summary>
/// <param name="requestResult">The RequestResult{JToken} response from ExecuteRequestAsync.</param>
/// <exception cref="ShopifyException">Thrown if <paramref name="requestResult"/> contains an error.</exception>
private void CheckForErrors(RequestResult<JToken> requestResult)
private static void CheckForErrors(RequestResult<JToken> requestResult)
{
if (requestResult.Result["errors"] != null)
if (requestResult.Result["errors"] is null)
{
var errorList = new List<string>();

foreach (var error in requestResult.Result["errors"])
return;
}

var errorList = new List<string>();

foreach (var error in requestResult.Result["errors"])
{
var errorMessage = error["message"];

if (errorMessage is not null)
{
errorList.Add(error["message"].ToString());
errorList.Add(errorMessage.ToString());
}
}

var message = requestResult.Result["errors"].FirstOrDefault()["message"].ToString();
var message = requestResult.Result["errors"].FirstOrDefault()?["message"]?.ToString();

throw new ShopifyException(requestResult.Response, HttpStatusCode.OK, errorList, message, requestResult.RawResult, "");
}
throw new ShopifyException(requestResult.Response, HttpStatusCode.OK, errorList, message, requestResult.RawResult, "");
}
}
}

0 comments on commit 756a209

Please sign in to comment.