Skip to content

Commit eb61477

Browse files
committed
Better error handling
1 parent 288cb46 commit eb61477

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

common/src/main/kotlin/com/lambda/command/commands/CapeCommand.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import com.lambda.brigadier.required
2525
import com.lambda.command.LambdaCommand
2626
import com.lambda.network.CapeManager.updateCape
2727
import com.lambda.network.NetworkManager
28-
import com.lambda.threading.runSafe
28+
import com.lambda.util.Communication.info
29+
import com.lambda.util.Communication.logError
2930
import com.lambda.util.extension.CommandBuilder
3031

3132
object CapeCommand : LambdaCommand(
@@ -44,9 +45,10 @@ object CapeCommand : LambdaCommand(
4445
}
4546

4647
execute {
47-
runSafe {
48-
val cape = id().value()
49-
updateCape(cape)
48+
val cape = id().value()
49+
updateCape(cape) { error ->
50+
if (error != null) logError("Could not update your cape", error)
51+
else info("Updated your cape to $cape")
5052
}
5153
}
5254
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ import net.minecraft.client.texture.NativeImageBackedTexture
3636
import java.io.ByteArrayOutputStream
3737
import java.util.UUID
3838
import java.util.concurrent.ConcurrentHashMap
39-
import kotlin.io.path.ExperimentalPathApi
4039
import kotlin.io.path.extension
4140
import kotlin.io.path.inputStream
4241
import kotlin.io.path.nameWithoutExtension
4342
import kotlin.io.path.walk
4443

45-
@OptIn(ExperimentalPathApi::class)
4644
@Suppress("JavaIoSerializableObjectMustHaveReadResolve")
4745
object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
4846
/**
@@ -55,22 +53,26 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
5553

5654
/**
5755
* Sets the current player's cape
56+
*
57+
* @param block Lambda called once the coroutine completes, it contains the throwable if any
5858
*/
59-
fun updateCape(cape: String, block: () -> Unit = {}) = runIO {
60-
setCape(cape)
61-
}.invokeOnCompletion { block() }
59+
fun updateCape(cape: String, block: (Throwable?) -> Unit = {}) = runIO {
60+
setCape(cape).getOrThrow()
61+
}.invokeOnCompletion { block(it) }
6262

6363
/**
6464
* Fetches the cape of the given player id
65+
*
66+
* @param block Lambda called once the coroutine completes, it contains the throwable if any
6567
*/
66-
fun SafeContext.fetchCape(uuid: UUID, block: () -> Unit = {}) = runIO {
68+
fun SafeContext.fetchCape(uuid: UUID, block: (Throwable?) -> Unit = {}) = runIO {
6769
val cape = getCape(uuid).getOrThrow()
6870

6971
mc.textureManager.get(cape.identifier) ?: download(cape)
7072
put(uuid, cape.id)
71-
}.invokeOnCompletion { block() }
73+
}.invokeOnCompletion { block(it) }
7274

73-
private fun SafeContext.download(cape: Cape, block: () -> Unit = {}) = runIO {
75+
private fun SafeContext.download(cape: Cape, block: (Throwable?) -> Unit = {}) = runIO {
7476
val destination = capes.resolveFile("${cape.id}.png")
7577
val output = ByteArrayOutputStream()
7678

@@ -84,7 +86,7 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
8486
val id = cape.identifier
8587

8688
mc.textureManager.registerTexture(id, native)
87-
}.invokeOnCompletion { block() }
89+
}.invokeOnCompletion { block(it) }
8890

8991
override fun load() = "Loaded ${images.size} cached capes"
9092

common/src/main/kotlin/com/lambda/network/api/v1/endpoints/SetCape.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ import io.ktor.http.*
3333
* response: [Unit] or error
3434
*/
3535
suspend fun setCape(id: String) = runCatching {
36-
LambdaHttp.put("$apiUrl/api/${apiVersion.value}/cape?id=$id") {
36+
val resp = LambdaHttp.put("$apiUrl/api/${apiVersion.value}/cape?id=$id") {
3737
bearerAuth(NetworkManager.accessToken)
3838
contentType(ContentType.Application.Json)
3939
}
40+
41+
check(resp.status == HttpStatusCode.OK)
4042
}

0 commit comments

Comments
 (0)