Click here to Skip to main content
14,875,168 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:

        public async Task Profileosu([Remainder]string username = null)
                string url = String.Format($"{k}&u={username}");
                using (HttpClient client = new HttpClient())
                        client.BaseAddress = new Uri("");
                        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);

                    embed.WithAuthor($"{username} #{profile.First().pp_rank}, {profile.First().pp_raw}PP", Context.Guild.CurrentUser.GetAvatarUrl(), $"{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());
                if (ex.InnerException != null)
                if (ex.InnerException.InnerException.Message != null)
Updated 29-Aug-20 3:53am
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.
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