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

Spring *ExceptionHandler not working with valiktor-spring-boot-starter #47

Open
dragneelfps opened this issue Jul 19, 2020 · 1 comment

Comments

@dragneelfps
Copy link

Expected Behavior

ConstraintViolationException are automatically handled by ReactiveConstraintViolationExceptionHandler

Actual Behavior

ConstraintViolationException are handled by ExceptionHandlingWebHandler

Steps to Reproduce the Problem

  1. Use spring kofu setup
  2. Add org.valiktor:valiktor-spring-boot-starter:0.12.0 to deps.
  3. Send request which throws ConstraintViolationException
PS C:\Users\srawa> Invoke-RestMethod "http://localhost:8000/api/user" -Body $User2 -Method POST -ContentType "application/json" | ConvertTo-Json

Invoke-RestMethod: {"timestamp":"2020-07-19T14:17:47.153+00:00","path":"/api/user","status":500,"error":"Internal Server Error","message":"","requestId":"64027368-1","exception":"org.valiktor.ConstraintViolationException"}

Server logs:

2020-07-19 19:47:47.166 ERROR 13776 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [64027368-1]  500 Server Error for HTTP POST "/api/user"

org.valiktor.ConstraintViolationException: null
	at io.nooblabs.eventmanagerbackend.User.validate(EventmanagerBackendApplication.kt:62) ~[main/:na]
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	|_ checkpoint ⇢ HTTP POST "/api/user" [ExceptionHandlingWebHandler]
Stack trace:

Example Code or Link to a Project

val app = reactiveWebApplication {
    beans {
        bean<UserHandler>()
        bean(::routes)
    }
    webFlux {
        port = if (profiles.contains("test")) 8081 else 8000
        codecs {
            string()
            jackson()
        }
    }
}

data class User(
        val login: String,
        val name: String
) {

    fun validate(): User {
        _validate(this) {
            validate(User::login)
                    .isNotBlank()
                    .hasSize(min = 4, max = 8)
            validate(User::name)
                    .isNotBlank()
                    .hasSize(max = 32)
        }
        return this
    }
}

class UserHandler {
    suspend fun createApi(request: ServerRequest): ServerResponse {
        return request.awaitBody<User>()
                .validate()
                .let { user -> ok().bodyValueAndAwait(user) }
    }
}

fun routes(userHandler: UserHandler) = coRouter {
    POST("/api/user", userHandler::createApi)
}


fun main() {
    app.run()
}

Environment

  • Valiktor version: 0.12.0
  • JDK/JRE version: adopt-openj9-14.0.1
  • Platform/OS: Win 10
@alc0der
Copy link

alc0der commented Mar 30, 2021

For me this only happens when invoking API from JUnit using @WebFluxTest; it works with @SpringBootTest.
I've tried to inject Beans for ValiktorExceptionHandler and ReactiveConstraintViolationExceptionHandler, but it made things worse.

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

No branches or pull requests

2 participants