fix(#372): plugin install/uninstall 400 — enable instead of install for on-disk plugins, gate uninstall behind canRemove, parse FastAPI error bodies (#416)

- Replace installPlugin(name) with activatePlugin(plugin) that calls enable endpoint
  instead of the install endpoint for plugins already on disk (bundled/inactive).
- Gate Uninstall button behind plugin.canRemove — bundled plugins don't show it.
- Update PluginsScreen to call activatePlugin(plugin) instead of installPlugin(name).
- Parse FastAPI 'detail' field from 400 response bodies in mapHttpError/safeApiCall
  so users see the actual error message instead of a generic placeholder.
- Import ButtonDefaults to fix max-line-length lint warning.

Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
M57andM57 authored and GitHub committed 2026-06-29 00:45:16 +03:00
1 parent b10495c6e2
commit 2ae6cce929
3 files changed
+44 -22

No files matched your search

@@ -1,5 +1,6 @@
package com.m57.hermescontrol.data.remote
import com.google.gson.JsonParser
import kotlinx.coroutines.delay
import retrofit2.Response
import java.io.IOException
@@ -37,12 +38,28 @@ sealed interface NetworkError {
) : NetworkError
}
fun mapHttpError(code: Int): NetworkError {
fun mapHttpError(
code: Int,
errorBody: String? = null,
): NetworkError {
if (code == 401) {
return NetworkError.AuthExpired()
}
// Try to parse FastAPI-style error detail from response body
val detail =
if (errorBody != null) {
try {
val parsed = com.google.gson.JsonParser.parseString(errorBody).asJsonObject
parsed.get("detail")?.asString
} catch (_: Exception) {
null
}
} else {
null
}
val message =
when (code) {
detail ?: when (code) {
400 -> "Bad Request (HTTP 400): The server could not understand the request."
403 -> "Forbidden (HTTP 403): You do not have permission to access this resource."
404 -> "Not Found (HTTP 404): The requested resource could not be found."
@@ -88,12 +105,19 @@ suspend inline fun <reified T> safeApiCall(
}
} else {
val code = response.code()
// Read error body for detail message
val errorBody =
try {
response.errorBody()?.string()
} catch (_: Exception) {
null
}
if ((code in 500..599 || code == 429) && attempt < retries) {
val backoff = jitteredBackoff(500L * (1 shl attempt))
delay(backoff)
continue
}
return NetworkResult.Failure(mapHttpError(code))
return NetworkResult.Failure(mapHttpError(code, errorBody))
}
} catch (e: IOException) {
lastException = e