Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
improve download error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
j2ghz committed Jun 30, 2018
1 parent 5d17fe8 commit 2cc1612
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 30 deletions.
5 changes: 2 additions & 3 deletions src/ModSink.Common/Client/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using ModSink.Core.Models.Group;
using ModSink.Core.Models.Repo;
using ReactiveUI;
using Serilog;

namespace ModSink.Common.Client
{
Expand Down Expand Up @@ -56,7 +55,7 @@ public async Task ScheduleMissingFilesDownload(Modpack modpack)
{
var fileSignature = fh.Value;
var (available, stream) = await LocalStorageService.WriteIfMissingOrInvalid(fileSignature);
Log.Debug("Check {fh}, Exists: {exists}", fileSignature.Hash, available);
LogTo.Debug("Check {fh}, Exists: {exists}", fileSignature.Hash, available);
if (!available)
DownloadService.Add(new Download(GetDownloadUri(fileSignature), stream,
fileSignature.ToString()));
Expand All @@ -74,7 +73,7 @@ public Uri GetDownloadUri(FileSignature fileSignature)
private async Task<T> Load<T>(Uri uri) where T : IBaseUri
{
LogTo.Information("Loading {T} from {url}", typeof(T), uri);
using (var mem = new MemoryStream())
using (var mem = new MemoryStream())
{
await Downloader.Download(uri, mem);
LogTo.Debug("Deserializing, size: {size}", mem.Length.Bytes().Humanize("G03"));
Expand Down
6 changes: 2 additions & 4 deletions src/ModSink.Common/Client/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System.Reactive.Disposables;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using Anotar.Serilog;
using Humanizer;
using Humanizer.Bytes;
using ModSink.Core.Client;
using ReactiveUI;
using Serilog;
using Stateless;

namespace ModSink.Common.Client
Expand All @@ -32,7 +32,7 @@ public Download(Uri source, Lazy<Task<Stream>> destination, string name, ulong e
state = new StateMachine<DownloadState, Trigger>(DownloadState.Queued);
state.OnTransitioned(_ => this.RaisePropertyChanged(nameof(State)));
state.OnTransitioned(t =>
log.Verbose("Switched to state {state}", t.Destination.Humanize()));
LogTo.Verbose("Switched to state {state}", t.Destination.Humanize()));
state.Configure(DownloadState.Queued)
.Permit(Trigger.Start, DownloadState.Downloading);
state.Configure(DownloadState.Downloading)
Expand All @@ -41,8 +41,6 @@ public Download(Uri source, Lazy<Task<Stream>> destination, string name, ulong e
.OnDeactivate(() => progressSubscription.Dispose());
}

private ILogger log => Log.ForContext<Download>().ForContext("ID", Name);

public Lazy<Task<Stream>> Destination { get; }
public string Name { get; }
public IObservable<DownloadProgress> Progress => progress;
Expand Down
5 changes: 2 additions & 3 deletions src/ModSink.Common/Client/DownloadService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Linq;
using System.Net;
using Anotar.Serilog;
using DynamicData;
using ModSink.Core.Client;
using Serilog;

namespace ModSink.Common.Client
{
Expand All @@ -12,7 +12,6 @@ public class DownloadService : IDownloadService
private readonly IDownloader downloader;
private readonly SourceList<IDownload> downloads = new SourceList<IDownload>();
private byte simultaneousDownloads;
private ILogger Log => Serilog.Log.ForContext<DownloadService>();

public DownloadService(IDownloader downloader)
{
Expand All @@ -34,7 +33,7 @@ public byte SimultaneousDownloads

public void Add(IDownload download)
{
Log.Debug("Scheduling {download}", download);
LogTo.Debug("Scheduling {download}", download);
downloads.Add(download);
CheckDownloadsToStart();
}
Expand Down
16 changes: 15 additions & 1 deletion src/ModSink.Common/HttpClientDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using Anotar.Serilog;
using Fusillade;
using Humanizer;
using Humanizer.Bytes;
Expand Down Expand Up @@ -32,7 +34,19 @@ public IConnectableObservable<DownloadProgress> Download(Uri source, Stream dest

//Read response
report(ByteSize.FromBytes(0), ByteSize.FromBytes(0), TransferState.ReadingResponse);
response.EnsureSuccessStatusCode();

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException)
{
if (response.StatusCode == HttpStatusCode.NotFound)
LogTo.Warning("Downloading '{url}' failed, server responded with 404 (NotFound)'",
source);
throw;
}

if (expectedLength != 0)
if (response.Content.Headers.ContentLength > 0)
if (Convert.ToUInt64(response.Content.Headers.ContentLength) != expectedLength)
Expand Down
2 changes: 1 addition & 1 deletion src/ModSink.Common/ModSink.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/j2ghz/ModSink.Common</PackageProjectUrl>
<Copyright>Copyright (C) 2017 Jozef Hollý</Copyright>
<LangVersion>latest</LangVersion>
<DebugType>embedded</DebugType>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/ModSink.WPF/View/ModpackView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ModpackView()
this.WhenActivated(d =>
{
this.OneWayBind(ViewModel, vm => vm.Modpack.Name, v => v.TbName.Text).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Size, v => v.TbSize).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Size, v => v.TbSize.Text).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Modpack.Mods, v => v.LbMods.ItemsSource).DisposeWith(d);
this.BindCommand(ViewModel, vm => vm.Download, v => v.BtnInstall).DisposeWith(d);
});
Expand Down
7 changes: 3 additions & 4 deletions src/ModSink.WPF/ViewModel/DownloadViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Linq;
using System.Reactive.Linq;
using Anotar.Serilog;
using Humanizer;
using ModSink.Core.Client;
using ReactiveUI;
using Serilog;

namespace ModSink.WPF.ViewModel
{
Expand Down Expand Up @@ -46,18 +46,17 @@ public DownloadViewModel(IDownload download)
LogErrors(state);
}

private ILogger log => Log.ForContext<DownloadViewModel>().ForContext("downloadName", Name);
public string State => state.Value;
public string Downloaded => downloaded.Value;
public string Name { get; }
public double Progress => progress.Value;
public string Size => size.Value;
public string Speed => speed.Value;

private void LogErrors(IHandleObservableErrors oaph)
private static void LogErrors(IHandleObservableErrors oaph)
{
oaph.ThrownExceptions.Subscribe(e =>
log.Error(e, "{downloadName} An exception from Observable of underlying download was caught"));
LogTo.Error(e, "{downloadName} An exception from Observable of underlying download was caught"));
}
}
}
21 changes: 8 additions & 13 deletions src/ModSink.WPF/ViewModel/DownloadsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using Anotar.Serilog;
using DynamicData;
using DynamicData.Aggregation;
using DynamicData.ReactiveUI;
using DynamicData.Binding;
using Humanizer;
using ModSink.Core.Client;
using ReactiveUI;
Expand All @@ -12,10 +12,7 @@ namespace ModSink.WPF.ViewModel
{
public class DownloadsViewModel : ReactiveObject
{
private readonly CompositeDisposable disposable = new CompositeDisposable();
private readonly ReactiveList<DownloadViewModel> downloads = new ReactiveList<DownloadViewModel>();
private readonly ObservableAsPropertyHelper<string> queueCount;
private string url;

public DownloadsViewModel(IClientService clientService)
{
Expand All @@ -24,24 +21,22 @@ public DownloadsViewModel(IClientService clientService)
.AutoRefresh(d => d.State)
.Filter(d => d.State == DownloadState.Downloading)
.Transform(d => new DownloadViewModel(d))
.Bind(downloads)
.DisposeMany()
.Bind(Downloads)
.Subscribe();
queueCount = ds
.AutoRefresh(d => d.State)
.Filter(d => d.State == DownloadState.Queued)
.Count()
.Select(i => "file".ToQuantity(i))
.ToProperty(this, t => t.QueueCount);
queueCount.ThrownExceptions.Subscribe(e => LogTo.Warning(e, "Download failed"));
}


public IReadOnlyReactiveList<DownloadViewModel> Downloads => downloads;
public string QueueCount => queueCount.Value;
public ObservableCollectionExtended<DownloadViewModel> Downloads { get; } =
new ObservableCollectionExtended<DownloadViewModel>();

public string Url
{
get => url;
set => this.RaiseAndSetIfChanged(ref url, value);
}
public string QueueCount => queueCount.Value;
}
}

0 comments on commit 2cc1612

Please sign in to comment.