Reputation: 820
I do have a problem with one case in Mule 3.9.
I have my API generated with .this .raml file.
One of endpoint is just GET with "/" route (for testing purposes).
In this flow (managing GET /) I would like to call another flow to check sth (In my case JWT validation). When is valid I would like to pass on, and return the origin payload. When not valid, I call an exception in Groovy, which indeed happens, but after all, it comes to the same origin payload, which shouldn't be visible.
Let's make some code:
origin flow:
<flow-ref name="validate_jwt_token" doc:name="Validate JWT"/>
<set-payload value=" { "message" : "API is working!"}" doc:name="Set Payload" mimeType="application/json" />
</flow>
and flow ref:
<flow name="validate_jwt_token">
<logger message="Request found with Authorization: #[message.inboundProperties['authorization']]" level="INFO" doc:name="Loger Authorization"/>
<component doc:name="Validate JWT Token">
<spring-object bean="ValidateJWTEvent"/>
</component>
<choice doc:name="Choice">
<when expression="(payload == true)">
<logger message="JWT valid. Access granted." level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<scripting:component doc:name="Throw Exception">
<scripting:script engine="Groovy"><![CDATA[throw new Exception('Invalid JWT Token.')]]></scripting:script>
</scripting:component>
</otherwise>
</choice>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-property propertyName="http.status" value="401" doc:name="Set 401 status"/>
<flow-ref name="commons.exceptionStrategy.logError" doc:name="Log errors"/>
</catch-exception-strategy>
</flow>
And even if this token is in fact invalid, I'm getting the same payload with text "API IS WORKING". What I want to do is to catch exception and return HTTP ERROR. Nothing more.
I'm attaching my Exception-Strategy as well. Please keep in mind, it is based in APIKIT, generated my Mule itselft .raml file.
<sub-flow name="commons.exceptionStrategy.logError">
<logger message="Error: #[exception.?cause.message or exception]" level="ERROR" doc:name="Logger" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Set Content Type"/>
<flow-ref name="log_params" doc:name="log_params"/>
<logger message="#[org.apache.commons.lang3.StringEscapeUtils.escapeJson(exception.?getCauseException().?getMessage())]" level="ERROR" doc:name="Display error"/>
<set-payload value="{ "responseHeader": { "requestId": "#[flowVars.storedRequestId]", "sendDate": "#[function:now]"}, "code": "#[flowVars.'statusCode']", "message": "#[flowVars.errorMessage or org.apache.commons.lang3.StringEscapeUtils.escapeJson(exception.?getCauseException().?getMessage())]" }" doc:name="Set Error Message"/>
</sub-flow>
<sub-flow name="log_params">
<logger message="API| endpoint executed with payload: #[message.payload]" level="INFO" doc:name="log payload"/>
<logger message="API| endpoint executed with flowVars: #[flowVars]" level="INFO" doc:name="log params"/>
</sub-flow>
The whole API is consumed by APIKIT my Mule with Global Catch Exception Strategy.
Please help me guys.
Upvotes: 0
Views: 724
Reputation: 2835
This looks like you are catching the exception in the same flow as you are throwing it. Therefore, it is not propagated and the flow is not interrupted since the exception is handled. Try using a rollback-exception-strategy instead, which will modify the payload but propagate the exception to the caller flow ref and interrupt that flow. HTH
Upvotes: 1