Fixed issues with exception handling for web calls

This commit is contained in:
djbadders 2019-09-21 23:43:13 +01:00
parent da511f2edd
commit 0c26066091
2 changed files with 26 additions and 21 deletions

View File

@ -48,26 +48,31 @@ namespace Core.MarketAnalyzer
catch (WebException ex) catch (WebException ex)
{ {
log.DoLogCritical(string.Format("Error whilst calling {0} \nError: {1}", url, ex.Message), ex); log.DoLogCritical(string.Format("Error whilst calling {0} \nError: {1}", url, ex.Message), ex);
// Error calling the service but we got a response so dump it.
string responseString = string.Empty;
var encoding = httpResponse.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(httpResponse.CharacterSet);
using (var stream = httpResponse.GetResponseStream()) if (ex.Response != null)
{ {
var reader = new StreamReader(stream, encoding); // Error calling the service but we got a response so dump it.
responseString = reader.ReadToEnd(); string responseString = string.Empty;
var response = ((HttpWebResponse)ex.Response);
var encoding = response.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(response.CharacterSet);
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream, encoding);
responseString = reader.ReadToEnd();
}
log.DoLogCritical(String.Format("{0} - Response: ({1}) {2} : {3}", ex.Message, response.StatusCode, response.StatusDescription, responseString), ex);
} }
log.DoLogCritical(String.Format("{0} - Response: ({1}) {2} : {3}", ex.Message, httpResponse.StatusCode, httpResponse.StatusDescription, responseString), ex); throw;
throw ex;
} }
catch (Exception ex) catch (Exception ex)
{ {
log.DoLogCritical(ex.Message, ex); log.DoLogCritical(ex.Message, ex);
}
return jsonObject; throw;
}
} }
public static Newtonsoft.Json.Linq.JObject GetSimpleJsonObjectFromURL(string url, LogHelper log, bool swallowException) public static Newtonsoft.Json.Linq.JObject GetSimpleJsonObjectFromURL(string url, LogHelper log, bool swallowException)
@ -351,15 +356,15 @@ namespace Core.MarketAnalyzer
} }
public static List<MarketTrendChange> GetMarketTrendChanges( public static List<MarketTrendChange> GetMarketTrendChanges(
string platform, string platform,
string mainMarket, string mainMarket,
MarketTrend marketTrend, MarketTrend marketTrend,
List<string> marketList, List<string> marketList,
Dictionary<string, Market> recentMarkets, Dictionary<string, Market> recentMarkets,
Dictionary<string, Market> trendMarkets, Dictionary<string, Market> trendMarkets,
string sortBy, string sortBy,
bool isGlobal, bool isGlobal,
PTMagicConfiguration systemConfiguration, PTMagicConfiguration systemConfiguration,
LogHelper log) LogHelper log)
{ {
List<MarketTrendChange> result = new List<MarketTrendChange>(); List<MarketTrendChange> result = new List<MarketTrendChange>();

View File

@ -416,7 +416,7 @@ namespace Core.MarketAnalyzer
Dictionary<string, Market> tickMarkets = new Dictionary<string, Market>(); Dictionary<string, Market> tickMarkets = new Dictionary<string, Market>();
foreach (string key in markets.Keys) foreach (string key in markets.Keys)
{ {
List<MarketTick> tickRange = marketTicks[key].FindAll(t => t.Time <= tickTime); List<MarketTick> tickRange = marketTicks[key] != null ? marketTicks[key].FindAll(t => t.Time <= tickTime) : new List<MarketTick>();
if (tickRange.Count > 0) if (tickRange.Count > 0)
{ {