- 
                Notifications
    You must be signed in to change notification settings 
- Fork 662
Open
Labels
Description
What is your use-case and why do you need this feature?
I have a Spring Boot REST API and I want to control what error codes are returned if there's a problem with some JSON.
At the moment I use Jackson, and in a @ControllerAdvice class I can handle the Jackson exceptions. They provide specific data about the JSON mapping failure.
@ControllerAdvice
class ControllerExceptionHandler {
    @ExceptionHandler(JsonProcessingException::class)
    fun handleJsonError(exception: JsonProcessingException): ResponseEntity<Any> {
        val error = when (exception) {
            // ...
            is UnrecognizedPropertyException -> "${cause.propertyName} unrecognised")
            // ...
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error)
    }I can't do this with Kotilnx Serialization because the exceptions are internal, and data about the exceptions gets concatenated into a string.
@ExceptionHandler(kotlinx.serialization.SerializationException::class)
fun handleKotlinxSerializationException(exception: SerializationException): ResponseEntity<Any> {
    when (exception) {
        // ERROR Cannot access 'MissingFieldException': it is internal in 'kotlinx.serialization'
        is kotlinx.serialization.MissingFieldException -> ...
    }
}Describe the solution you'd like
I'd like to do the same with the Kotlinx Serialization exceptions. Here are some suggestions:
- make the existing exceptions public, and add fields so I can access the exact problem
@PublishedApi public class MissingFieldException( // make public val fieldNames: List<String>, // expose the specific problem val cause: Exception?, ) : SerializationException("Field ... was missing...", cause) 
- Keep the exceptions internal, but provide some public data class and add it to SerializationExceptionAnd add the detail to the existing public exceptionsealed interface SerializationExceptionDetail { data class MissingFields( val missingFields: List<String> ): SerializationExceptionDetail data class UnknownField( val unknownFields: List<String> ): SerializationExceptionDetail // ... } public open class SerializationException( val detail: SerializationExceptionDetail ) : IllegalArgumentException { 
Related:
Intex32, DSeeLP, cweinberger, qwwdfsad, OfekTeken and 5 more