Skip to content

Commit

Permalink
Fixed error when calling bad Azure url.
Browse files Browse the repository at this point in the history
  • Loading branch information
valdetero committed Jun 26, 2015
1 parent 8f58fc4 commit 6c36972
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 10 deletions.
22 changes: 22 additions & 0 deletions SevenDays.Core/Helpers/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SevenDays.Core.Interfaces;

namespace SevenDays.Core.Helpers
{
public class Logger : ILogger
{
public void LogException(Exception exception)
{
Xamarin.Insights.Report(exception);
}

public void Track(string trackIdentifier)
{
Xamarin.Insights.Track(trackIdentifier);
}
}
}
14 changes: 14 additions & 0 deletions SevenDays.Core/Interfaces/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SevenDays.Core.Interfaces
{
public interface ILogger
{
void LogException(Exception exception);
void Track(string trackIdentifier);
}
}
33 changes: 24 additions & 9 deletions SevenDays.Core/Services/SevendayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public class SevendayService : ISevendayService
private readonly INetworkService networkService;
private readonly ICacheService cache;
private readonly ISettings settings;
private readonly ILogger logger;
public SevendayService()
{
networkService = Ioc.Container.Resolve<INetworkService>();
cache = Ioc.Container.Resolve<ICacheService>();
settings = Ioc.Container.Resolve<ISettings>();
logger = Ioc.Container.Resolve<ILogger>();
}

private async Task<string> getApiUrlAsync(string api)
Expand All @@ -46,19 +48,19 @@ private async Task<string> getApiUrlAsync(string api)
return await cache.GetObject<SevenDays.Model.Entity.Server>(serverKey);
}

public async Task<bool> CanConnectToServer(string host, string port)
public Task<bool> CanConnectToServer(string host, string port)
{
return await networkService.CanConnectToService(string.Format("http://{0}", host), port);
return networkService.CanConnectToService(string.Format("http://{0}", host), port);
}

public async Task<bool> CanConnectToServer(SevenDays.Model.Entity.Server server)
public Task<bool> CanConnectToServer(SevenDays.Model.Entity.Server server)
{
if (server == null)
return false;
return Task.FromResult(false);
if (string.IsNullOrEmpty(server.Host) || string.IsNullOrEmpty(server.Port))
return false;
return Task.FromResult(false);

return await CanConnectToServer(server.Host, server.Port);
return CanConnectToServer(server.Host, server.Port);
}

public async Task<bool> CanConnectToServer()
Expand Down Expand Up @@ -98,10 +100,23 @@ public async Task<ListResponse<Player>> GetPlayersLocation()

string url = await getApiUrlAsync(ApiConstants.Seven.PlayerLocation);

using (var client = new HttpClient(new NativeMessageHandler()))
try
{
var result = await client.GetStringAsync(url);
response.Result = JsonConvert.DeserializeObject<IEnumerable<Player>>(result);
using (var client = new HttpClient(new NativeMessageHandler()))
{
var result = await client.GetStringAsync(url);
response.Result = JsonConvert.DeserializeObject<IEnumerable<Player>>(result);
}
}
/*
System.Net.WebExceptionStatus.ReceiveFailure = 3
Azure throws this error. However this enum doesn't exist in the namespace.
*/
catch (System.Net.WebException ex) when ((int)ex.Status == 3)
{
if ((int) ex.Status != 3)
throw;
logger.LogException(ex);
}

return response;
Expand Down
2 changes: 2 additions & 0 deletions SevenDays.Core/SevenDays.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
<Compile Include="Helpers\AsyncErrorHandler.cs" />
<Compile Include="Helpers\Grouping.cs" />
<Compile Include="Helpers\InsightsAttribute.cs" />
<Compile Include="Helpers\Logger.cs" />
<Compile Include="Helpers\RelayCommand.cs" />
<Compile Include="Helpers\Settings.cs" />
<Compile Include="Interfaces\ICacheService.cs" />
<Compile Include="Interfaces\ILogger.cs" />
<Compile Include="Interfaces\INetworkService.cs" />
<Compile Include="Interfaces\ISettings.cs" />
<Compile Include="Interfaces\ISevendayService.cs" />
Expand Down
1 change: 1 addition & 0 deletions SevenDays.Shared/IoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static void Init()
Container.Register<ISevendayService>(() => new SevendayService());
Container.Register<ISettings>(() => new Settings());
Container.Register<ICacheService>(() => new AkavacheCacheService());
Container.Register<ILogger>(() => new Logger());
}
}
}
28 changes: 28 additions & 0 deletions SevenDays.Tests/SevenDays.Tests.Shared/LoggerMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using SevenDays.Core.Interfaces;

namespace SevenDays.Tests.Shared
{
public class LoggerMock : ILogger
{
public void LogException(Exception exception)
{
if (exception != null)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
while (exception.InnerException != null)
{
exception = exception.InnerException;
System.Diagnostics.Debug.WriteLine(exception.Message);
}
}
}

public void Track(string trackIdentifier)
{
System.Diagnostics.Debug.WriteLine($"Tracked: {trackIdentifier}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)CacheServiceMock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LoggerMock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NetworkServiceMock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NetworkServiceTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SettingsMock.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using SevenDays.Model.Base;
using SevenDays.Model.Seven;
using Xunit;
//using Settings = SevenDays.Core.Helpers.Settings;

namespace SevenDays.Tests.Shared
{
Expand All @@ -22,6 +21,7 @@ public SevendayServiceTests()
Container.Register<INetworkService>(() => new NetworkServiceMock());
Container.Register<ICacheService>(() => new CacheServiceMock());
Container.Register<ISettings>(() => new SettingsMock());
Container.Register<ILogger>(() => new LoggerMock());
Container.Register<ISevendayService>(() => new SevendayService());
}

Expand Down

0 comments on commit 6c36972

Please sign in to comment.