Skip to content

Commit 1d99fe0

Browse files
committed
Better downloading error handling
1 parent 842f68d commit 1d99fe0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

common/src/main/kotlin/com/lambda/network/LambdaHttp.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ val LambdaHttp = HttpClient {
3535
}
3636

3737
suspend inline fun HttpClient.download(url: String, file: File, block: HttpRequestBuilder.() -> Unit = {}) {
38-
val response = get(url, block).readRawBytes()
39-
file.writeBytes(response)
38+
val response = get(url, block)
39+
check(response.status.isSuccess()) { "Download for $url failed with non 2xx status code" }
40+
41+
file.writeBytes(response.readRawBytes())
4042
}
4143

4244
suspend inline fun HttpClient.download(url: String, output: OutputStream, block: HttpRequestBuilder.() -> Unit = {}) {
43-
val response = get(url, block).readRawBytes()
44-
output.write(response)
45+
val response = get(url, block)
46+
check(response.status.isSuccess()) { "Download for $url failed with non 2xx status code" }
47+
48+
output.write(response.readRawBytes())
4549
}
4650

4751
suspend inline fun HttpClient.download(url: String, block: HttpRequestBuilder.() -> Unit) =

0 commit comments

Comments
 (0)