Skip to content

Commit

Permalink
httpService added try catch for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeByLudwig committed Apr 17, 2023
1 parent a33a9a6 commit 12531ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion DiscordBot/Services/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ public HttpService(IRestClient httpClient)
request.AddJsonBody(JsonConvert.DeserializeObject(jsonBodyString) ?? "");
}

var response = new RestResponse();

// Send the HTTP request asynchronously and await the response.
var response = await _httpClient.ExecuteAsync(request);
try
{
response = await _httpClient.ExecuteAsync(request);
}
catch (Exception e)
{
response.IsSuccessStatusCode = false;
response.ErrorMessage = $"({nameof(GetResponseFromUrl)}): Unknown error occurred" + e.Message;
response.ErrorException = e;
response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
}

var content = response.Content;

if (!response.IsSuccessStatusCode)
Expand Down
4 changes: 2 additions & 2 deletions DiscordBot/Services/OpenWeatherMapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public OpenWeatherMapService(IHttpService httpService, IConfiguration configurat
public async Task<(bool Success, string Message, WeatherData? weatherData)> GetWeatherAsync(string city)
{
// Retrieve the url and the apikey from the configuration
_openWeatherMapUrl = _configuration["OpenWeatherMap:ApiKey"] ?? string.Empty;
_openWeatherMapApiKey = _configuration["OpenWeatherMap:ApiUrl"] ?? string.Empty;
_openWeatherMapUrl = _configuration["OpenWeatherMap:ApiUrl"] ?? string.Empty;
_openWeatherMapApiKey = _configuration["OpenWeatherMap:ApiKey"] ?? string.Empty;

if (string.IsNullOrEmpty(_openWeatherMapApiKey) || string.IsNullOrEmpty(_openWeatherMapUrl))
{
Expand Down

0 comments on commit 12531ec

Please sign in to comment.