Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose exception details so I can handle specific errors differently, and customise the error message #1930

Open
aSemy opened this issue May 6, 2022 · 0 comments
Labels

Comments

@aSemy
Copy link
Contributor

aSemy commented May 6, 2022

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:

  1. 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)
  2. Keep the exceptions internal, but provide some public data class and add it to SerializationException
    sealed interface SerializationExceptionDetail {
        data class MissingFields(
            val missingFields: List<String>
        ): SerializationExceptionDetail
        
        data class UnknownField(
            val unknownFields: List<String>
        ): SerializationExceptionDetail
        
        // ...
    }
    And add the detail to the existing public exception
    public open class SerializationException(
      val detail: SerializationExceptionDetail
    ) : IllegalArgumentException {

Related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant