https://kotlinlang.org logo
#http4k
Title
# http4k
d

dmcg

12/13/2023, 10:11 AM
I see
Copy code
fun <T> AutoMappingConfiguration<T>.withStandardMappings() = apply {
    text(duration())
    text(uri())
    text(url())
    text(uuid())
    text(regexObject())
    text(instant())
    text(yearMonth())
    text(localTime())
    text(localDate())
    text(localDateTime())
    text(zonedDateTime())
    text(offsetTime())
    text(offsetDateTime())
    text(zoneId())
    text(zoneOffset())
    text(eventCategory())
    text(traceId())
    text(samplingDecision())
    text(throwable())
    text(locale())
    int({ Status(it, "") }, Status::code)
}
which is really handy, but I’d like to change the handling for just Throwables. Can anyone thing of a good way to override the standards? Context - I have some `Event`s that are also `Exception`s (try it, it’s really handy), but they are currently rendered with
.toString()
not as structured objects
d

dave

12/13/2023, 10:35 AM
Moshi has a the concept of
addLast
. Not sure if there is the equivalent in Jackson, but might be worth having a look
🙏 1
d

dmcg

12/13/2023, 2:34 PM
Wait what?
Copy code
fun throwable() = BiDiMapping({ throw Exception(it) }, Throwable::asString)
Deserializing a throwable throws an exception?
d

dave

12/13/2023, 2:35 PM
yes - because you cannot deserialise an exception!
d

dmcg

12/13/2023, 2:35 PM
🤯
a

Andrew O'Hara

12/13/2023, 3:03 PM
Not with that attitude! I don't really see why you shouldn't 🤔 . Apart from it being a silly idea and all 🙃 .
d

dmcg

12/13/2023, 3:06 PM
I suppose the issue is that it is serializing Throwable, and who knows what subclass it should deserialize. Feels a bit more like
error("Cannot deserialize a Throwable with message $it")
but I can see the current is less typing
a

Andrew O'Hara

12/13/2023, 3:08 PM
Yeah, usually the BiDiMappings deal with a concrete class. This is the first time I've noticed one using an abstract class. So it would never be possible to get the same type back. I assume what you're trying to do is add your own mapper to override this default behaviour?
d

dmcg

12/13/2023, 3:20 PM
Well what I really want to do is render a data class that is also an Exception as the data class fields, not the Throwable.toString()