Skip to content

Commit

Permalink
Merge pull request #10 from kjetilmk/bugfix/throw-on-network-error
Browse files Browse the repository at this point in the history
Bugfix/throw on network error
  • Loading branch information
Tarjei Olsen authored Nov 22, 2019
2 parents 85ba67b + e57aa29 commit ac28431
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 47 deletions.
71 changes: 25 additions & 46 deletions src/EpiserverAdapter/Communication/HttpClientInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Epinova.InRiverConnector.Interfaces;
using inRiver.Integration.Logging;
Expand Down Expand Up @@ -51,63 +52,41 @@ public async Task<string> Get(string uri)

public async Task PostAsync<T>(string url, T message)
{
try
{
IntegrationLogger.Write(LogLevel.Debug, $"Posting to {url}");
Stopwatch timer = Stopwatch.StartNew();
IntegrationLogger.Write(LogLevel.Debug, $"Posting to {url}");
Stopwatch timer = Stopwatch.StartNew();

HttpResponseMessage response = await HttpClient.PostAsJsonAsync(url, message);
response.EnsureSuccessStatusCode();
HttpResponseMessage response = await HttpClient.PostAsJsonAsync(url, message);
response.EnsureSuccessStatusCode();

IntegrationLogger.Write(LogLevel.Debug, $"Posted to {url}, took {timer.ElapsedMilliseconds}.");
}
catch (Exception ex) when (
ex is TaskCanceledException ||
ex is HttpRequestException)
{
IntegrationLogger.Write(LogLevel.Error, "Unable to connect to episerver, trying agian..");
await Task.Delay(15000);
await PostAsync(url, message);
}
IntegrationLogger.Write(LogLevel.Debug, $"Posted to {url}, took {timer.ElapsedMilliseconds}.");
}

public async Task<string> PostWithAsyncStatusCheck<T>(string url, T message)
{
try
{
IntegrationLogger.Write(LogLevel.Debug, $"Posting to {url}");

HttpResponseMessage response = await HttpClient.PostAsJsonAsync(url, message);

if (response.IsSuccessStatusCode)
{
string parsedResponse = await response.Content.ReadAsAsync<string>();
IntegrationLogger.Write(LogLevel.Debug, $"Posting to {url}");

while (parsedResponse == ImportStatus.IsImporting)
{
await Task.Delay(15000);
parsedResponse = await Get(_isImportingAction);
}
HttpResponseMessage response = await HttpClient.PostAsJsonAsync(url, message);

if (parsedResponse.StartsWith("ERROR"))
IntegrationLogger.Write(LogLevel.Error, parsedResponse);
if (response.IsSuccessStatusCode)
{
string parsedResponse = await response.Content.ReadAsAsync<string>();

return parsedResponse;
while (parsedResponse == ImportStatus.IsImporting)
{
Thread.Sleep(15000);
parsedResponse = await Get(_isImportingAction);
}

string errorMsg = $"Import failed: {(int)response.StatusCode} ({response.ReasonPhrase})";
IntegrationLogger.Write(LogLevel.Error, errorMsg);
}
catch (Exception ex) when (
ex is TaskCanceledException ||
ex is HttpRequestException)
{
IntegrationLogger.Write(LogLevel.Error, "Unable to connect to episerver, trying again..");
await Task.Delay(15000);
return await PostWithAsyncStatusCheck(url, message);
if (parsedResponse.StartsWith("ERROR"))
IntegrationLogger.Write(LogLevel.Error, parsedResponse);

return parsedResponse;
}

return "$Posting to {url} failed";
string errorMsg = $"Import failed: {(int) response.StatusCode} ({response.ReasonPhrase})";
IntegrationLogger.Write(LogLevel.Error, errorMsg);

throw new HttpRequestException(errorMsg);
}

public List<string> PostWithStringListAsReturn<T>(string url, T message)
Expand All @@ -119,9 +98,9 @@ public List<string> PostWithStringListAsReturn<T>(string url, T message)

if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<List<string>>().Result;
string errorMsg = $"Import failed: {(int)response.StatusCode} ({response.ReasonPhrase})";
string errorMsg = $"Import failed: {(int) response.StatusCode} ({response.ReasonPhrase})";
IntegrationLogger.Write(LogLevel.Error, errorMsg);
throw new HttpRequestException(errorMsg);
}
}
}
}
2 changes: 1 addition & 1 deletion src/EpiserverImporter/CatalogImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private void MoveNode(string nodeCode, int newParent)
CatalogNodeDto catalogNodeDto = CatalogContext.Current.GetCatalogNodeDto(nodeCode, new CatalogNodeResponseGroup(CatalogNodeResponseGroup.ResponseGroup.CatalogNodeFull));

// Move node to new parent
_logger.Debug($"Move {nodeCode} to new parent ({newParent}).");
_logger.Information($"Move {nodeCode} to new parent ({newParent}).");
catalogNodeDto.CatalogNode[0].ParentNodeId = newParent;
CatalogContext.Current.SaveCatalogNode(catalogNodeDto);
}
Expand Down

0 comments on commit ac28431

Please sign in to comment.