3

Suppose there's a Double variable in an action, and if the value sent in the request body is something like

{"dblField":""}

and the interceptorStack looks like :

<action name="save" class="actions.MyAction" method="save">
    <interceptor-ref name="jsonValidationWorkflowStack">
    </interceptor-ref>
    <!--<interceptor-ref name="loginStack"/>-->
   <!-- I've tried using each of the above two separately, but both failed -->
    <interceptor-ref name="json">
        <param name="enableSMD">true</param>
    </interceptor-ref>
    
    <result type="json" name="*">
        <param name="excludeProperties">
            idIo
        </param>
    </result>
</action>

Then the action throws a NumberFormatException, which is fine according to the plugin source code here.

But this exception is not handled in the plugin and hence, returns from the action throwing exception, which results in the firing of global-exception-handler.

If the same request was sent using a query-string, ?dblField= then the action returns INPUT.

So, how can I make the json-plugin behave in the same way to return INPUT and set appropriate fieldErrors instead of throwing NumberFormatException and firing the globalExceptionHandler?

Roman C
  • 49,761
  • 33
  • 66
  • 176
coding_idiot
  • 13,526
  • 10
  • 65
  • 116

1 Answers1

2

You could place exception interceptor before your own interceptor instead of the json interceptor by extending json interceptor and override intercept method where you can catch errors. Then you can either add action errors or rethrow a custom exception which you can map in the action config or globally.

<exception-mapping exception="org.exceptionmapping.CustomException"
                             result="errorresult"/>

This way you can map all json only interceptor errors with your custom exception.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It's different. Validate() is not the one which is calling the jsonToObject converter, that's done by the JSON interceptor, which throws NumberFormatException because of blank double value. – coding_idiot Jan 06 '14 at 16:14
  • It too much work, if I had to write a validation interceptor before the JSON interceptor, instead the JSON interceptor or the json-validation interceptor should add Errors, rather than throwing Exception which is then handled by globalExceptionHandler. I'm also working on creating a SSCCE that'll help to greatly understand the situation. – coding_idiot Jan 06 '14 at 16:53
  • I can't check for errors, because the JSON interceptor throws a NFE and the action code is never executed. Even though a double is passed a string, it shouldn't throw Exception instead return INPUT, adding the error to fieldErrors. Is there any other means to check for errors ? – coding_idiot Jan 06 '14 at 17:51
  • finally, I ended up writing my own JSONInterceptor, but that's not good, since it's unable to log/add fieldErrors. It'll work for now, I've filed it as a bug, let's see what those guys think – coding_idiot Jan 14 '14 at 08:13
  • finally, I've finished with JSON validation framework which could be integrated whatsoever in the application via extension point or via filter or interceptor. – Roman C Jun 14 '20 at 18:34