Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 28a741b

Browse files
Better to throw an exception when things don't work out
1 parent 2800d7f commit 28a741b

File tree

2 files changed

+73
-45
lines changed

2 files changed

+73
-45
lines changed

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
9090
}
9191
}
9292

93-
class DownloadTask : TaskBase<bool>
93+
class DownloadTask : TaskBase
9494
{
9595
private readonly IFileSystem fileSystem;
9696
private long bytes;
97-
private WebRequest webRequest;
9897
private bool restarted;
9998

10099
public float Progress { get; set; }
@@ -110,19 +109,19 @@ public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url,
110109
Name = "DownloadTask";
111110
}
112111

113-
protected override bool RunWithReturn(bool success)
112+
protected override void Run(bool success)
114113
{
115-
base.RunWithReturn(success);
114+
base.Run(success);
116115

117116
RaiseOnStart();
118117

119-
var result = false;
120118
var attempts = 0;
121119
try
122120
{
121+
bool result;
123122
do
124123
{
125-
Logger.Trace($"Download of {Url} to {Destination} Attempt {attempts + 1} of {RetryCount + 1}");
124+
Logger.Trace($"Download of {Url} Attempt {attempts + 1} of {RetryCount + 1}");
126125
result = Download();
127126
if (result && ValidationHash != null)
128127
{
@@ -140,20 +139,23 @@ protected override bool RunWithReturn(bool success)
140139
break;
141140
}
142141
}
143-
} while (RetryCount < attempts++);
142+
} while (attempts++ < RetryCount);
143+
144+
if (!result)
145+
{
146+
throw new DownloadException("Error downloading file");
147+
}
144148
}
145149
catch (Exception ex)
146150
{
147151
Errors = ex.Message;
148-
if (!RaiseFaultHandlers(ex))
152+
if (!RaiseFaultHandlers(new DownloadException("Error downloading file", ex)))
149153
throw;
150154
}
151155
finally
152156
{
153-
RaiseOnEnd(result);
157+
RaiseOnEnd();
154158
}
155-
156-
return result;
157159
}
158160

159161
protected virtual void UpdateProgress(float progress)
@@ -178,51 +180,42 @@ public bool Download()
178180
}
179181
}
180182

181-
webRequest = WebRequest.Create(Url);
182-
var httpWebRequest = webRequest as HttpWebRequest;
183-
if (httpWebRequest != null)
183+
var expectingResume = restarted && bytes > 0;
184+
185+
var webRequest = (HttpWebRequest)WebRequest.Create(Url);
186+
187+
if (expectingResume)
184188
{
185-
if (bytes > 0)
186-
{
187-
// TODO: fix classlibs to take long overloads
188-
httpWebRequest.AddRange((int)bytes);
189-
}
189+
// TODO: fix classlibs to take long overloads
190+
webRequest.AddRange((int)bytes);
190191
}
191192

192193
webRequest.Method = "GET";
193194
webRequest.Timeout = 3000;
194195

195-
if (restarted && bytes > 0)
196+
if (expectingResume)
196197
Logger.Trace($"Resuming download of {Url} to {Destination}");
197198
else
198199
Logger.Trace($"Downloading {Url} to {Destination}");
199200

200-
using (var webResponse = webRequest.GetResponseWithoutException())
201+
using (var webResponse = (HttpWebResponse) webRequest.GetResponseWithoutException())
201202
{
202-
if (webResponse == null)
203-
return false;
203+
var httpStatusCode = webResponse.StatusCode;
204+
Logger.Trace($"Downloading {Url} StatusCode:{(int)webResponse.StatusCode}");
204205

205-
if (restarted && bytes > 0)
206+
if (expectingResume && httpStatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
206207
{
207-
var httpWebResponse = webResponse as HttpWebResponse;
208-
if (httpWebResponse != null)
209-
{
210-
var httpStatusCode = httpWebResponse.StatusCode;
211-
if (httpStatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
212-
{
213-
UpdateProgress(1);
214-
return true;
215-
}
208+
UpdateProgress(1);
209+
return true;
210+
}
216211

217-
if (!(httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.PartialContent))
218-
{
219-
return false;
220-
}
221-
}
212+
if (!(httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.PartialContent))
213+
{
214+
return false;
222215
}
223216

224217
var responseLength = webResponse.ContentLength;
225-
if (restarted && bytes > 0)
218+
if (expectingResume)
226219
{
227220
UpdateProgress(bytes / (float)responseLength);
228221
}
@@ -249,6 +242,15 @@ public bool Download()
249242
protected int RetryCount { get; }
250243
}
251244

245+
class DownloadException : Exception
246+
{
247+
public DownloadException(string message) : base(message)
248+
{ }
249+
250+
public DownloadException(string message, Exception innerException) : base(message, innerException)
251+
{ }
252+
}
253+
252254
class DownloadTextTask : TaskBase<string>
253255
{
254256
public float Progress { get; set; }

src/tests/IntegrationTests/Download/DownloadTaskTests.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public async Task TestDownloadTask()
2626
var downloadHalfPath = TestBasePath.Combine("5MB-split.zip");
2727

2828
var downloadTask = new DownloadTask(CancellationToken.None, fileSystem, TestDownload, downloadPath);
29-
var downloadResult = await downloadTask.StartAwait();
30-
31-
downloadResult.Should().BeTrue();
29+
await downloadTask.StartAwait();
3230

3331
var downloadPathBytes = fileSystem.ReadAllBytes(downloadPath);
3432
Logger.Trace("File size {0} bytes", downloadPathBytes.Length);
@@ -45,9 +43,7 @@ public async Task TestDownloadTask()
4543
fileSystem.WriteAllBytes(downloadHalfPath, cutDownloadPathBytes);
4644

4745
downloadTask = new DownloadTask(CancellationToken.None, fileSystem, TestDownload, downloadHalfPath, TestDownloadMD5, 1);
48-
downloadResult = await downloadTask.StartAwait();
49-
50-
downloadResult.Should().BeTrue();
46+
await downloadTask.StartAwait();
5147

5248
var downloadHalfPathBytes = fileSystem.ReadAllBytes(downloadHalfPath);
5349
Logger.Trace("File size {0} Bytes", downloadHalfPathBytes.Length);
@@ -56,6 +52,36 @@ public async Task TestDownloadTask()
5652
md5Sum.Should().Be(TestDownloadMD5.ToUpperInvariant());
5753
}
5854

55+
[Test]
56+
public void TestDownloadFailure()
57+
{
58+
InitializeTaskManager();
59+
60+
var fileSystem = new FileSystem();
61+
62+
var downloadPath = TestBasePath.Combine("5MB.zip");
63+
64+
var taskFailed = false;
65+
Exception exceptionThrown = null;
66+
67+
var autoResetEvent = new AutoResetEvent(false);
68+
69+
var downloadTask = new DownloadTask(CancellationToken.None, fileSystem, "http://www.unknown.com/5MB.gz", downloadPath, null, 1)
70+
.Finally((b, exception) => {
71+
taskFailed = !b;
72+
exceptionThrown = exception;
73+
autoResetEvent.Set();
74+
});
75+
76+
downloadTask.Start();
77+
78+
autoResetEvent.WaitOne();
79+
80+
taskFailed.Should().BeTrue();
81+
exceptionThrown.Should().NotBeNull();
82+
}
83+
84+
5985
[Test]
6086
public void TestDownloadTextTask()
6187
{

0 commit comments

Comments
 (0)