Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error handling #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions FeedBuilder/FeedBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ProductVersion />
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{734FA44E-39AC-478E-A5AA-0F3D1F1974AA}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<StartupObject>FeedBuilder.Program</StartupObject>
<RootNamespace>FeedBuilder</RootNamespace>
<AssemblyName>FeedBuilder</AssemblyName>
Expand Down Expand Up @@ -51,7 +51,8 @@
<OptionInfer>On</OptionInfer>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>component.ico</ApplicationIcon>
<ApplicationIcon>
</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
Expand Down
4 changes: 4 additions & 0 deletions src/NAppUpdate.Framework/NAppUpdate.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
5 changes: 4 additions & 1 deletion src/NAppUpdate.Framework/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/NAppUpdate.Framework/Sources/IUpdateSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace NAppUpdate.Framework.Sources
public interface IUpdateSource
{
string GetUpdatesFeed(); // TODO: return a the feed as a stream
bool GetData(string filePath, string basePath, Action<UpdateProgressInfo> onProgress, ref string tempLocation);
void GetData(string filePath, string basePath, Action<UpdateProgressInfo> onProgress, ref string tempLocation);
}
}
12 changes: 6 additions & 6 deletions src/NAppUpdate.Framework/Sources/MemorySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public string GetUpdatesFeed()
return Feed;
}

public bool GetData(string filePath, string basePath, Action<UpdateProgressInfo> onProgress, ref string tempFile)
public void GetData(string filePath, string basePath, Action<UpdateProgressInfo> onProgress, ref string tempFile)
{
Uri uriKey = null;

Expand All @@ -37,12 +37,12 @@ public bool GetData(string filePath, string basePath, Action<UpdateProgressInfo>
else if (Uri.IsWellFormedUriString(basePath, UriKind.Absolute))
uriKey = new Uri(new Uri(basePath, UriKind.Absolute), filePath);

if (uriKey == null || !tempFiles.ContainsKey(uriKey))
return false;
if (uriKey == null)
throw new ApplicationException($"Unable to create Uri where filePath is '{filePath}' and basePath is '{basePath}'");
if (!tempFiles.ContainsKey(uriKey))
throw new ApplicationException($"Uri '${uriKey}' not found in tempFiles");

tempFile = tempFiles[uriKey];

return true;
tempFile = tempFiles[uriKey];
}

#endregion
Expand Down
13 changes: 10 additions & 3 deletions src/NAppUpdate.Framework/Sources/SimpleWebSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public string GetUpdatesFeed()
return data;
}

public bool GetData(string url, string baseUrl, Action<UpdateProgressInfo> onProgress, ref string tempLocation)
public void GetData(string url, string baseUrl, Action<UpdateProgressInfo> onProgress, ref string tempLocation)
{
FileDownloader fd;
// A baseUrl of http://testserver/somefolder with a file linklibrary.dll was resulting in a webrequest to http://testserver/linklibrary
Expand All @@ -85,8 +85,15 @@ public bool GetData(string url, string baseUrl, Action<UpdateProgressInfo> onPro
// files requiring pre-processing
tempLocation = Path.GetTempFileName();

return fd.DownloadToFile(tempLocation, onProgress);
}
try
{
fd.DownloadToFile(tempLocation, onProgress);
}
catch (Exception ex)
{
throw new ApplicationException($"An error occurred while downloading file '{(fd as FileDownloader).Uri.ToString()}'. {ex.Message}");
}
}

#endregion
}
Expand Down
3 changes: 1 addition & 2 deletions src/NAppUpdate.Framework/Sources/UncSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public string GetUpdatesFeed()
return data;
}

public bool GetData(string filePath, string basePath, Action<UpdateProgressInfo> onProgress, ref string tempLocation)
public void GetData(string filePath, string basePath, Action<UpdateProgressInfo> onProgress, ref string tempLocation)
{
if (basePath == null)
{
Expand All @@ -62,7 +62,6 @@ public bool GetData(string filePath, string basePath, Action<UpdateProgressInfo>
}

File.Copy(basePath + filePath, tempLocation);
return true;
}
}
}
14 changes: 10 additions & 4 deletions src/NAppUpdate.Framework/Tasks/FileUpdateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ public override void Prepare(Sources.IUpdateSource source)

UpdateManager.Instance.Logger.Log("FileUpdateTask: Downloading {0} with BaseUrl of {1} to {2}", fileName, baseUrl, tempFileLocal);

if (!source.GetData(fileName, baseUrl, OnProgress, ref tempFileLocal))
throw new UpdateProcessFailedException("FileUpdateTask: Failed to get file from source");

_tempFile = tempFileLocal;
try
{
source.GetData(fileName, baseUrl, OnProgress, ref tempFileLocal);
}
catch (Exception ex)
{
throw new UpdateProcessFailedException($"FileUpdateTask: Failed to get file from source. {ex.Message}");
}

_tempFile = tempFileLocal;
if (_tempFile == null)
throw new UpdateProcessFailedException("FileUpdateTask: Failed to get file from source");

Expand Down
6 changes: 3 additions & 3 deletions src/NAppUpdate.Framework/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ public void PrepareUpdates()
catch (Exception ex)
{
task.ExecutionStatus = TaskExecutionStatus.FailedToPrepare;
Logger.Log(ex);
throw new UpdateProcessFailedException("Failed to prepare task: " + task.Description, ex);
Logger.Log(Logger.SeverityLevel.Warning, $"Error while preparing file with LocalPath '{(task as FileUpdateTask).LocalPath}'. {ex.Message}");
}
Logger.Dump();

task.ExecutionStatus = TaskExecutionStatus.Prepared;
task.ExecutionStatus = TaskExecutionStatus.Prepared;
}

State = UpdateProcessState.Prepared;
Expand Down
Binary file modified src/NAppUpdate.Framework/Updater/updater.exe
Binary file not shown.
97 changes: 59 additions & 38 deletions src/NAppUpdate.Framework/Utils/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public sealed class FileDownloader
private const int _bufferSize = 1024;
public IWebProxy Proxy { get; set; }

public Uri Uri
{
get
{
return _uri;
}
}

public FileDownloader()
{
Proxy = null;
Expand All @@ -33,46 +41,59 @@ public byte[] Download()
return client.DownloadData(_uri);
}

public bool DownloadToFile(string tempLocation)
{
return DownloadToFile(tempLocation, null);
}
public void DownloadToFile(string tempLocation)
{
DownloadToFile(tempLocation, null);
}

public bool DownloadToFile(string tempLocation, Action<UpdateProgressInfo> onProgress)
public void DownloadToFile(string tempLocation, Action<UpdateProgressInfo> onProgress)
{
var request = WebRequest.Create(_uri);
request.Proxy = Proxy;

using (var response = request.GetResponse())
using (var tempFile = File.Create(tempLocation))
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
return false;

long downloadSize = response.ContentLength;
long totalBytes = 0;
var buffer = new byte[_bufferSize];
const int reportInterval = 1;
DateTime stamp = DateTime.Now.Subtract(new TimeSpan(0, 0, reportInterval));
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
totalBytes += bytesRead;
tempFile.Write(buffer, 0, bytesRead);

if (onProgress == null || !(DateTime.Now.Subtract(stamp).TotalSeconds >= reportInterval)) continue;
ReportProgress(onProgress, totalBytes, downloadSize);
stamp = DateTime.Now;
} while (bytesRead > 0 && !UpdateManager.Instance.ShouldStop);

ReportProgress(onProgress, totalBytes, downloadSize);
return totalBytes == downloadSize;
}
}
}
var request = WebRequest.Create(_uri);
request.Proxy = Proxy;

try
{
using (var response = request.GetResponse())
using (var tempFile = File.Create(tempLocation))
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
throw new ApplicationException($"Unable to get response stream to file at uri '{_uri}'");

long downloadSize = response.ContentLength;
long totalBytes = 0;
var buffer = new byte[_bufferSize];
const int reportInterval = 1;
DateTime stamp = DateTime.Now.Subtract(new TimeSpan(0, 0, reportInterval));
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
totalBytes += bytesRead;
tempFile.Write(buffer, 0, bytesRead);

if (onProgress == null || !(DateTime.Now.Subtract(stamp).TotalSeconds >= reportInterval)) continue;
ReportProgress(onProgress, totalBytes, downloadSize);
stamp = DateTime.Now;
} while (bytesRead > 0 && !UpdateManager.Instance.ShouldStop);

ReportProgress(onProgress, totalBytes, downloadSize);

if (totalBytes != downloadSize)
throw new ApplicationException($"File at uri '{_uri}' did not fully download");
}
}
}
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound)
{
throw new ApplicationException($"Unable to download file at '{_uri}'. {ex.Message}");
}
catch (Exception ex)
{
throw new ApplicationException($"Unable to download file at '{_uri}'. {ex.Message}");
}
}

private void ReportProgress(Action<UpdateProgressInfo> onProgress, long totalBytes, long downloadSize)
{
Expand Down