commit 07aae8e4f3aa373f8b78bb58ee56595810a797bd
parent 76c51a54761c7e22fdb63ffd880ad661e205682c
Author: Antoine A <>
Date: Thu, 17 Oct 2024 17:41:35 +0200
common: code cleanup
Diffstat:
3 files changed, 15 insertions(+), 20 deletions(-)
diff --git a/common/src/main/kotlin/ApiError.kt b/common/src/main/kotlin/ApiError.kt
@@ -105,6 +105,9 @@ fun unauthorized(
fun internalServerError(hint: String?): ApiException
= apiError(HttpStatusCode.InternalServerError, hint, TalerErrorCode.GENERIC_INTERNAL_INVARIANT_FAILURE)
+fun paramsMalformed(hint: String): ApiException
+ = badRequest(hint, TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+
fun notFound(
hint: String,
error: TalerErrorCode
diff --git a/common/src/main/kotlin/TalerCommon.kt b/common/src/main/kotlin/TalerCommon.kt
@@ -50,14 +50,6 @@ data class TalerProtocolTimestamp(
@Serializable(with = Serializer::class)
val t_s: Instant,
) {
- companion object {
- fun fromMicroseconds(uSec: Long): TalerProtocolTimestamp {
- return TalerProtocolTimestamp(
- Instant.EPOCH.plus(uSec, ChronoUnit.MICROS)
- )
- }
- }
-
internal object Serializer : KSerializer<Instant> {
override fun serialize(encoder: Encoder, value: Instant) {
if (value == Instant.MAX) {
diff --git a/common/src/main/kotlin/params.kt b/common/src/main/kotlin/params.kt
@@ -26,11 +26,11 @@ import java.util.*
fun Parameters.expect(name: String): String
= get(name) ?: throw badRequest("Missing '$name' parameter", TalerErrorCode.GENERIC_PARAMETER_MISSING)
fun Parameters.int(name: String): Int?
- = get(name)?.run { toIntOrNull() ?: throw badRequest("Param '$name' not a number", TalerErrorCode.GENERIC_PARAMETER_MALFORMED) }
+ = get(name)?.run { toIntOrNull() ?: throw paramsMalformed("Param '$name' not a number") }
fun Parameters.expectInt(name: String): Int
= int(name) ?: throw badRequest("Missing '$name' number parameter", TalerErrorCode.GENERIC_PARAMETER_MISSING)
fun Parameters.long(name: String): Long?
- = get(name)?.run { toLongOrNull() ?: throw badRequest("Param '$name' not a number", TalerErrorCode.GENERIC_PARAMETER_MALFORMED) }
+ = get(name)?.run { toLongOrNull() ?: throw paramsMalformed("Param '$name' not a number") }
fun Parameters.expectLong(name: String): Long
= long(name) ?: throw badRequest("Missing '$name' number parameter", TalerErrorCode.GENERIC_PARAMETER_MISSING)
fun Parameters.uuid(name: String): UUID? {
@@ -38,7 +38,7 @@ fun Parameters.uuid(name: String): UUID? {
try {
UUID.fromString(this)
} catch (e: Exception) {
- throw badRequest("Param '$name' not an UUID", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ throw paramsMalformed("Param '$name' not an UUID")
}
}
}
@@ -49,7 +49,7 @@ fun Parameters.amount(name: String): TalerAmount?
try {
TalerAmount(this)
} catch (e: Exception) {
- throw badRequest("Param '$name' not a taler amount", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ throw paramsMalformed("Param '$name' not a taler amount")
}
}
@@ -61,18 +61,18 @@ data class PageParams(
val legacy_limit_value = params.int("delta")
val new_limit_value = params.int("limit")
if (legacy_limit_value != null && new_limit_value != null && legacy_limit_value != new_limit_value)
- throw badRequest("Param 'limit' cannot be used with param 'delta'", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ throw paramsMalformed("Param 'limit' cannot be used with param 'delta'")
val legacy_offset_value = params.long("start")
val new_offset_value = params.long("offset")
if (legacy_offset_value != null && new_offset_value != null && legacy_offset_value != new_offset_value)
- throw badRequest("Param 'offset' cannot be used with param 'start'", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ throw paramsMalformed("Param 'offset' cannot be used with param 'start'")
val limit: Int = new_limit_value ?: legacy_limit_value ?: -20
- if (limit == 0) throw badRequest("Param 'limit' must be non-zero", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
- else if (limit > MAX_PAGE_SIZE) throw badRequest("Param 'limit' must be <= ${MAX_PAGE_SIZE}", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ if (limit == 0) throw paramsMalformed("Param 'limit' must be non-zero")
+ else if (limit > MAX_PAGE_SIZE) throw paramsMalformed("Param 'limit' must be <= ${MAX_PAGE_SIZE}")
val offset: Long = new_offset_value ?: legacy_offset_value ?: if (limit >= 0) 0L else Long.MAX_VALUE
- if (offset < 0) throw badRequest("Param 'offset' must be a positive number", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ if (offset < 0) throw paramsMalformed("Param 'offset' must be a positive number")
return PageParams(limit, offset)
}
@@ -88,7 +88,7 @@ data class TransferParams(
fun extract(params: Parameters): TransferParams {
val status = params["status"]?.let {
if (!names.contains(it)) {
- throw badRequest("Param 'status' must be one of $names_fmt", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ throw paramsMalformed("Param 'status' must be one of $names_fmt")
}
TransferStatusState.valueOf(it)
}
@@ -105,9 +105,9 @@ data class PollingParams(
val legacy_value = params.long("long_poll_ms")
val new_value = params.long("timeout_ms")
if (legacy_value != null && new_value != null && legacy_value != new_value)
- throw badRequest("Param 'timeout_ms' cannot be used with param 'long_poll_ms'", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ throw paramsMalformed("Param 'timeout_ms' cannot be used with param 'long_poll_ms'")
val timeout_ms: Long = min(new_value ?: legacy_value ?: 0, MAX_TIMEOUT_MS)
- if (timeout_ms < 0) throw badRequest("Param 'timeout_ms' must be a positive number", TalerErrorCode.GENERIC_PARAMETER_MALFORMED)
+ if (timeout_ms < 0) throw paramsMalformed("Param 'timeout_ms' must be a positive number")
return PollingParams(timeout_ms)
}
}