commit 74ac18e0fe91acda97b190b3f12cb7e6584363d0
parent 7f645d734d45b7919985ec2bc6b06cd6618a8ef2
Author: Antoine A <>
Date: Thu, 20 Mar 2025 12:20:49 +0100
Improve error codes
Diffstat:
4 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/bank/src/test/kotlin/SecurityTest.kt b/bank/src/test/kotlin/SecurityTest.kt
@@ -65,22 +65,22 @@ class SecurityTest {
// Check body too big
client.postA("/accounts/merchant/transactions") {
json(too_big)
- }.assertBadRequest()
+ }.assertPayloadTooLarge()
// Check body too big even after compression
client.postA("/accounts/merchant/transactions") {
jsonDeflate(too_big)
- }.assertBadRequest()
+ }.assertPayloadTooLarge()
// Check streaming body too big
client.postA("/accounts/merchant/transactions") {
jsonStream(too_big)
- }.assertBadRequest()
+ }.assertPayloadTooLarge()
// Check streaming body too big even after compression
client.postA("/accounts/merchant/transactions") {
jsonStreamDeflate(too_big)
- }.assertBadRequest()
+ }.assertPayloadTooLarge()
// Check unknown encoding
client.postA("/accounts/merchant/transactions") {
diff --git a/common/src/main/kotlin/ApiError.kt b/common/src/main/kotlin/ApiError.kt
@@ -148,4 +148,9 @@ fun unsupportedMediaType(
fun notImplemented(
hint: String = "API not implemented",
error: TalerErrorCode = TalerErrorCode.END,
-): ApiException = apiError(HttpStatusCode.NotImplemented, hint, error)
-\ No newline at end of file
+): ApiException = apiError(HttpStatusCode.NotImplemented, hint, error)
+
+fun bodyOverflow(
+ hint: String,
+ error: TalerErrorCode = TalerErrorCode.GENERIC_UPLOAD_EXCEEDS_LIMIT,
+): ApiException = apiError(HttpStatusCode.PayloadTooLarge, hint, error)
+\ No newline at end of file
diff --git a/common/src/main/kotlin/api/server.kt b/common/src/main/kotlin/api/server.kt
@@ -54,7 +54,7 @@ fun bodyLimitPlugin(logger: Logger): ApplicationPlugin<Unit> {
// Check content length if present and wellformed
val contentLenght = call.request.headers[HttpHeaders.ContentLength]?.toIntOrNull()
if (contentLenght != null && contentLenght > MAX_BODY_LENGTH)
- throw badRequest("Body is suspiciously big > ${MAX_BODY_LENGTH}B")
+ throw bodyOverflow("Body is suspiciously big > ${MAX_BODY_LENGTH}B")
// Else check while reading and decompressing the body
transformBody { body ->
@@ -78,7 +78,7 @@ fun bodyLimitPlugin(logger: Logger): ApplicationPlugin<Unit> {
}
}
if (read > MAX_BODY_LENGTH)
- throw badRequest("Decompressed body is suspiciously big > ${MAX_BODY_LENGTH}B")
+ throw bodyOverflow("Decompressed body is suspiciously big > ${MAX_BODY_LENGTH}B")
}
}
null -> {
@@ -88,7 +88,7 @@ fun bodyLimitPlugin(logger: Logger): ApplicationPlugin<Unit> {
if (new == -1) break // Channel is closed
read += new
if (read > MAX_BODY_LENGTH)
- throw badRequest("Body is suspiciously big > ${MAX_BODY_LENGTH}B")
+ throw bodyOverflow("Body is suspiciously big > ${MAX_BODY_LENGTH}B")
}
}
else -> throw unsupportedMediaType(
diff --git a/common/src/main/kotlin/client.kt b/common/src/main/kotlin/client.kt
@@ -115,3 +115,6 @@ suspend fun HttpResponse.assertNotImplemented(err: TalerErrorCode = TalerErrorCo
= assertStatus(HttpStatusCode.NotImplemented, err)
suspend fun HttpResponse.assertTooManyRequests(err: TalerErrorCode): HttpResponse
= assertStatus(HttpStatusCode.TooManyRequests, err)
+suspend fun HttpResponse.assertPayloadTooLarge(
+ err: TalerErrorCode = TalerErrorCode.GENERIC_UPLOAD_EXCEEDS_LIMIT
+): HttpResponse = assertStatus(HttpStatusCode.PayloadTooLarge, err)
+\ No newline at end of file