Sign In Register

How can we help you today?

Start a new topic

REST API error handling in C#

Hello,


I've been trying to make API calls for game snapshot copying which work very well.


The only issue I'm having is the inconsistency of response in the "GET: /rest/games/{gameApiKey}/snapshot/list" 


The problem is that if the game key I want the snapshots from is correct it will return a json array with an object for each snapshot. But if they game key is wrong it will return a json object with the errorCode and the responseMessage.


This would be fine if I were not writing C#. The problem is that I have to use a library to deserialize the json response from string to a class. I would like to error handle in case something breaks but due to the inconsistency in the response I can't deserialize to the proper class and I would like to avoid checking for deserialization errors and then repeating the process to get the error.


Using the response error that returns also doesn't work because upon further inspection if something is wrong and you return the error message, the GET response is still 200 OK so I can't catch it that way either.


Any suggestions or fixes for this?


Thank you!


Regards,

Mike :D


1 person likes this idea

Bump?

Just wanting to verify that I correctly understand your issue:


The library you're using to deserialize the json string expects the string to have a specific structure so that it can correctly translate it into a specific class. The class specified works fine with a successful request, but when you're getting an errormessage it means the json is no longer structured so that the library can convert it, is this correct?


Could you perform a basic check on the string to see if it contains the term 'errorMessage' before attempting to parse the string into an object:


if(!response.Contains("errorMessage")


or something similar.

Do you think this is suitable for your situation?


Regards,

Vinnie

Login to post a comment