Example #1
0
        public async Task <WeatherModel> GetByCity(string country, string city)
        {
            var errors = new List <ErrorModel>();

            foreach (var provider in _providers)
            {
                try
                {
                    var res = await provider.GetWeather(country, city);

                    if (!res.Errored)
                    {
                        return(res);
                    }

                    errors.Add(res.Error);
                }
                catch (Exception ex)
                {
                    _logger.Warning(ex, "Provider exception. Type: {providerType}, Method: {method}", provider.GetType().Name, nameof(provider.GetWeather));
                    errors.Add(ErrorModel.FromException(ex, $"Exception from {provider.GetType().Name}"));
                }
            }

            //If we have reached here nothing was returned

            var errorMessage = $"One or more errors occurred:\n{string.Join("\n", errors.Select(e => e.ErrorMessage))}";
            var exceptions   = errors.Select(e => e.Exception);

            return(WeatherModel.FromException(new AggregateException(errorMessage, exceptions)));
        }
Example #2
0
        private WeatherModel MapData(string data)
        {
            if (string.Equals("Data not found", data, StringComparison.OrdinalIgnoreCase))
            {
                return(WeatherModel.FromException(new Exception("No data found")));
            }

            //TODO: Never got a succesful response back
            //... Happy path not done
            throw new NotImplementedException();
        }
Example #3
0
        public virtual async Task<WeatherModel> GetByCity(string country, string city)
        {
            WeatherModel model;

            try
            {
                model = await _provider.GetWeather(country, city);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "GetByCity failed. Country : {country}, City: {city}", country, city);
                model = WeatherModel.FromException(ex);
            }

            return model;
        }
Example #4
0
 public Task <WeatherModel> GetByCity(string country, string city)
 {
     return(Task.FromResult(WeatherModel.FromException(new Exception("An expected error has occurred"))));
 }