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

Commit

Permalink
improved download error reporting and increased timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
sargunv committed Apr 18, 2020
1 parent 56bcfd8 commit 7cc889d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
7 changes: 7 additions & 0 deletions modsman-core/src/main/kotlin/modsman/exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ sealed class ModsmanException : RuntimeException {
constructor(message: String, cause: Throwable) : super(message, cause)
}

class DownloadException(url: String, fileName: String, cause: Throwable) :
ModsmanException(
"Failed to download '$fileName' from '$url'"
+ ", caused by ${cause::class.java.simpleName}: ${cause.message}",
cause
)

class PinnedException(mod: ModEntry) :
ModsmanException("Won't upgrade pinned mod '${mod.projectName}'")

Expand Down
22 changes: 17 additions & 5 deletions modsman-core/src/main/kotlin/modsman/modsman.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.Closeable
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class Modsman(
modsPath: Path,
Expand All @@ -24,6 +26,12 @@ class Modsman(

private val ModEntry.filePath get() = modlist.modsPath.resolve(fileName)

private val okHttpClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build()

private fun chooseBestFile(files: List<CurseforgeFile>): CurseforgeFile {
val ret = files
.filter { file ->
Expand All @@ -50,11 +58,15 @@ class Modsman(
}

private suspend fun download(url: String, fileName: String) = withContext(Dispatchers.IO) {
Files.copy(
OkHttpClient().newCall(Request.Builder().url(url).build()).execute().body()!!.byteStream(),
modlist.modsPath.resolve(fileName),
StandardCopyOption.REPLACE_EXISTING
)
try {
Files.copy(
okHttpClient.newCall(Request.Builder().url(url).build()).execute().body()!!.byteStream(),
modlist.modsPath.resolve(fileName),
StandardCopyOption.REPLACE_EXISTING
)
} catch (e: IOException) {
throw DownloadException(url, fileName, e)
}
}

private fun readToBytes(jarPath: Path): ByteArray {
Expand Down

0 comments on commit 7cc889d

Please sign in to comment.