Top-gg-Community / dotnet-sdk

MIT License
32 stars 11 forks source link

Unexpected character met when parsing the API #5

Closed Earu closed 3 years ago

Earu commented 5 years ago

Hello, I've noticed that times to times the json serializer fails when trying to read received payloads. I get the following exception:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Miki.Net.Http.HttpClient.SendAsync[T](RequestMethod method, String url, String value)
   at Miki.Net.Http.HttpClient.GetAsync[T](String url)
   at DiscordBotsList.Api.DiscordBotListApi.GetAsync[T](String url)
   at DiscordBotsList.Api.DiscordBotListApi.GetBotAsync[T](UInt64 id)
   at DiscordBotsList.Api.AuthDiscordBotListApi.GetMeAsync()
   at Energize.EnergizeClient.UpdateDBLServerCount() in /home/earu/Energize/Energize/EnergizeClient.cs:line 110
   at Energize.EnergizeClient.<InitializeAsync>b__31_0(Object arg) in /home/earu/Energize/Energize/EnergizeClient.cs:line 136
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---

Here are the calls that caused the above exception:

            int servercount = this.DiscordClient.Guilds.Count;
            if (!this.IsDevEnv)
            {
                IDblSelfBot me = await this._DiscordBotList.GetMeAsync();
                await me.UpdateStatsAsync(servercount);
            }

You can find the up-to-date code at https://github.com/Earu/Energize/blob/master/Energize/EnergizeClient.cs#L105

This most likely happen when the API is down or unreachable.

velddev commented 5 years ago

Yeah, this means the API does not return valid JSON. and should handled gracefully in the request. 😄