elifarley
03/09/2018, 10:37 AMHTTP/1.1 400 body 'body' must be object
This message is hiding an exception. How can I get hold of it?
I have these filters:
routes(
"/sgzr/api/v1" bind Api.router(productService, orderService)
).let {
DebuggingFilters.PrintRequestAndResponse()
.then(ServerFilters.Cors(CorsPolicy.UnsafeGlobalPermissive))
.then(ServerFilters.CatchAll())
.then(ServerFilters.CatchLensFailure)
.then(it)
}
dave
03/09/2018, 10:44 AMobject CatchLensFailure : Filter {
override fun invoke(next: HttpHandler): HttpHandler = {
try {
next(it)
} catch (lensFailure: LensFailure) {
when {
lensFailure.target is Response -> throw lensFailure
lensFailure.target is RequestContext -> throw lensFailure
lensFailure.overall() == Failure.Type.Unsupported -> Response(UNSUPPORTED_MEDIA_TYPE)
else -> Response(BAD_REQUEST.description(lensFailure.failures.joinToString("; ")))
}
}
}
}
elifarley
03/09/2018, 11:00 AM.then(ServerFilters.CatchLensFailure)
.then(ServerFilters.CatchAll())
Before this, only HTTP/1.1 400 body 'body' must be object
would appear
By doing this, a nice stack trace appears on stdout (onlyHTTP/1.1 500 Internal Server Error
org.http4k.lens.LensFailure: body 'body' must be object
at org.http4k.contract.ContractRouteSpec$invoke$1.invoke(routeSpec.kt:31)
at org.http4k.contract.ContractRouteSpec$invoke$1.invoke(routeSpec.kt:11)
at org.http4k.core.Http4kKt$then$2.invoke(Http4k.kt:19)
[,...]
InvalidJSONException: Could not convert to a JSON Object or Array
. I want to have this information readily available. I'll follow your suggestion of creating a custom CatchLensFailuredave
03/09/2018, 11:55 AMelifarley
03/09/2018, 11:55 AMdave
03/09/2018, 11:56 AMoverride fun String.asJsonObject(): JsonElement = JsonParser().parse(this).let { if(it.isJsonArray || it.isJsonObject) it else throw InvalidJsonException() }
elifarley
03/09/2018, 12:00 PMdave
03/09/2018, 12:01 PMelifarley
03/09/2018, 12:01 PMdave
03/09/2018, 12:01 PMelifarley
03/09/2018, 12:02 PMthrow InvalidJsonException("Not an array nor an object: $it")
dave
03/09/2018, 12:04 PMelifarley
03/09/2018, 12:05 PMopen class LensFailure(val failures: List<Failure>, override val cause: Exception? = null, val target: Any? = null)
: Exception(failures.joinToString { "$it" } + (cause?.let { " ($it)" } ?: ""), cause) {
dave
03/09/2018, 12:20 PMelifarley
03/09/2018, 12:21 PMdave
03/09/2018, 12:21 PMelifarley
03/09/2018, 12:21 PM