diff options
author | xioren <40043405+xioren@users.noreply.github.com> | 2021-03-11 23:09:39 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-12 08:09:39 +0100 |
commit | 3d198fdcc237aa3927ad5c3b934688de24499b03 (patch) | |
tree | d9600fe1f0776778b3b6167378bbfc6e73067715 | |
parent | 23393b847836007313dd95b87d7691129ac44bd3 (diff) | |
download | Nim-3d198fdcc237aa3927ad5c3b934688de24499b03.tar.gz |
Relocate 4xx/5xx exception in downloadFile (#17332) [backport:1.2]
Move 4xx/5xx exception to before disk i/o. As it stands an empty file is created on http error 4xx/5xx.
-rw-r--r-- | lib/pure/httpclient.nim | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 9c39cb53b..4f5a33ee7 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -1219,6 +1219,9 @@ proc downloadFile*(client: HttpClient, url: Uri | string, filename: string) = defer: client.getBody = true let resp = client.get(url) + + if resp.code.is4xx or resp.code.is5xx: + raise newException(HttpRequestError, resp.status) client.bodyStream = newFileStream(filename, fmWrite) if client.bodyStream.isNil: @@ -1226,14 +1229,14 @@ proc downloadFile*(client: HttpClient, url: Uri | string, filename: string) = parseBody(client, resp.headers, resp.version) client.bodyStream.close() - if resp.code.is4xx or resp.code.is5xx: - raise newException(HttpRequestError, resp.status) - proc downloadFileEx(client: AsyncHttpClient, url: Uri | string, filename: string): Future[void] {.async.} = ## Downloads `url` and saves it to `filename`. client.getBody = false let resp = await client.get(url) + + if resp.code.is4xx or resp.code.is5xx: + raise newException(HttpRequestError, resp.status) client.bodyStream = newFutureStream[string]("downloadFile") var file = openAsync(filename, fmWrite) @@ -1248,9 +1251,6 @@ proc downloadFileEx(client: AsyncHttpClient, # `bodyStream` has been written to the file. await file.writeFromStream(client.bodyStream) - if resp.code.is4xx or resp.code.is5xx: - raise newException(HttpRequestError, resp.status) - proc downloadFile*(client: AsyncHttpClient, url: Uri | string, filename: string): Future[void] = result = newFuture[void]("downloadFile") |