ResponseStatusException not handled by Spring

Viewed 72

In a mixed Groovy/Java Spring web project, I am trying to validate an endpoint parameter inside a Groovy function like this (in a Groovy class):

import org.springframework.web.server.ResponseStatusException
[...]

private static addThreshold(Query query, Float threshold) {

    if (threshold != null) {
        if (-1.0 <= threshold && threshold <= 1.0) {
            query.threshold = threshold
        }
        else {
            throw new ResponseStatusException(
                    HttpStatus.BAD_REQUEST, "'threshold' value must be between 1.0 and -1.0."
            )
        }
    }
}

My controller has a this function:

@RequestMapping(method = GET, value = "/search")
@RequireSubscription([...])
def search(...) {
   [...]
}

The goal is that addThreshold() throws a ResponseStatusException so that the server returns a 400 error to the client, when threshold parameter is not between -1.0 and 1.0.

Instead, however, I get this error on the server side:

org.springframework.web.server.ResponseStatusException: 400 BAD_REQUEST "'threshold' value must be between 1.0 and -1.0."
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:72)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:286)
    at com.mypackage.MyClass.addThreshold(MyClass.groovy:544)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:149)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:100)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:217)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:240)
[...]

On the client, I get a 500 error with the default fallback message defined in my project:

{
    "errors": [
        {
            "code": "serverError",
            "info": null,
            "message": "An internal server error occurred. Please contact support"
        }
    ]
}

I see in the server logs that the expected exception is thrown. Why does Spring not return a 400 error with the generated reason?

Spring version is 5.2.8.

0 Answers
Related