Click here to Skip to main content
14,827,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My discord bot sometimes throws an exceptions that
An error occurred while sending the request
and I don't know how to fix it and more importantly what should I do. I've set if inner exception is not null then write that out too but it's empty because there is no other information about the exception

What I have tried:

C#
[Command("stats")]
        public async Task Profileosu([Remainder]string username = null)
        {
            try
            {
                string url = String.Format($"http://osu.ppy.sh/api/get_user?k={k}&u={username}");
                using (HttpClient client = new HttpClient())
                {
                        client.BaseAddress = new Uri("http://osu.ppy.sh");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        HttpResponseMessage responseMessage = await client.GetAsync(url);
                        
                         string result = await responseMessage.Content.ReadAsStringAsync();
                         IList<Profile> profile = JsonConvert.DeserializeObject<IList<Profile>>(result);
                   

                    //embedbuild
                    embed.WithThumbnailUrl($"https://a.ppy.sh/{profile.First().user_id}");
                    embed.WithAuthor($"{username} #{profile.First().pp_rank}, {profile.First().pp_raw}PP", Context.Guild.CurrentUser.GetAvatarUrl(), $"https://osu.ppy.sh/users/{profile.First().user_id}");
                    embed.WithDescription($"Join date:{profile.First().join_date}\nCountry:{profile.First().country} #{profile.First().pp_country_rank}\nAccuray:{double.Parse(profile.First().accuracy):F2}%\nPlaycount:{profile.First().playcount}");
                    embed.WithColor(154, 255, 0);
                    await ReplyAsync($"", false, embed.Build());

                }

            }
            catch (Exception ex)
            {

                embed.WithAuthor("An error occured");
                embed.WithDescription("This player doesn't exist, please check the spelling of the username!");
                embed.WithColor(255, 0, 0);
                await ReplyAsync($"", false, embed.Build());
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
                if (ex.InnerException.InnerException.Message != null)
                {
                    Console.WriteLine(ex.InnerException.InnerException.Message);
                }
            }
        }
Posted
Updated 29-Aug-20 3:53am
Comments
dnxit 4-Sep-19 2:11am
   
It's better to log the full Exception object to see for stack trace and other details but if you just want the message try ex.GetBaseException().Message;

It seems your "sometimes" factor is the "username"; as indicated in your exception handler.

I propose that "invalid user names" are NOT exceptions and should be pre-screened with a proper validation / sign-in procedure.
   
Comments
Citrom67 8-Sep-19 13:41pm
   
But then why does the same username work the very next time?
Why I can't send friend request
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900