mule mapping-exception-strategy 仅落入第一个 401 分支
mule mapping-exception-strategy only falls through to first 401 branch
向此询问一些 mulesoft 专业知识。
以下异常映射策略应该在 hhtp.status 401、403、429 上分支,但继续落入状态代码 401 和 403 的 401 分支(至少,由调试和日志确定写入控制台):
<apikit:mapping-exception-strategy doc:name="waysact-adaptor-main-exception-strategy">
<apikit:mapping statusCode="401">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 401 = #[payload]" level="INFO" doc:name="log-http-401"/>
</apikit:mapping>
<apikit:mapping statusCode="403">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 403 = #[payload]" level="INFO" doc:name="log-http-403"/>
</apikit:mapping>
<apikit:mapping statusCode="429">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 429 = #[payload]" level="INFO" doc:name="log-http-429"/>
</apikit:mapping>
<apikit:mapping statusCode="400">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging anything = #[payload]" level="INFO" doc:name="logging-anything"/>
</apikit:mapping>
</apikit:mapping-exception-strategy>
这是因为它仅在异常类型 org.mule.module.http.internal.request.ResponseValidatorException 上分支吗?我以为它是为了在状态代码上分支?
还有另一种策略,choice-exception-strategy,它应该在不同的异常对象类型上分支。
APIkit 根据定义的异常值匹配异常,而不是根据定义的statusCode。使用选择异常策略并在其中定义多个捕获异常策略。确保每个捕获异常策略都匹配一个唯一的异常,以便为正确的 http.status 代码和所需的异常负载铺平道路。
例如,如果要抛出 400,请确保捕获异常策略与 BadrequestException 匹配。如果状态代码来自 HTTP 请求者,则匹配 ResponseValidatorException 并设置传入 http.status 和异常负载
基于 http.status 的异常分支可以使用选择异常策略定义,如下例所示,
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy when="#[message.inboundProperties.'http.status'=='404']" doc:name="Catch Exception Strategy" >
<logger message="Exceptions message is ... #[exception.message]" level="ERROR" doc:name="exceptionLogger"/>
<set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
<set-payload value="{ errors: { errorCode: #[message.inboundProperties.'http.status'], errorMessage: #[flowVars.exceptionMessage] } }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>
<catch-exception-strategy doc:name="Catch Exception Strategy" >
<logger message="Exception message is ... #[exception.message]" level="ERROR" category="com.project.stacktrace" doc:name="exceptionLogger"/>
<set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
<set-payload value="{ errors: { errorCode: #[message.inboundProperties.'http.status'], errorMessage: #[flowVars.exceptionMessage] } }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>
</choice-exception-strategy>
向此询问一些 mulesoft 专业知识。
以下异常映射策略应该在 hhtp.status 401、403、429 上分支,但继续落入状态代码 401 和 403 的 401 分支(至少,由调试和日志确定写入控制台):
<apikit:mapping-exception-strategy doc:name="waysact-adaptor-main-exception-strategy">
<apikit:mapping statusCode="401">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 401 = #[payload]" level="INFO" doc:name="log-http-401"/>
</apikit:mapping>
<apikit:mapping statusCode="403">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 403 = #[payload]" level="INFO" doc:name="log-http-403"/>
</apikit:mapping>
<apikit:mapping statusCode="429">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging 429 = #[payload]" level="INFO" doc:name="log-http-429"/>
</apikit:mapping>
<apikit:mapping statusCode="400">
<apikit:exception value="org.mule.module.http.internal.request.ResponseValidatorException"/>
<logger message="psc>>> logging anything = #[payload]" level="INFO" doc:name="logging-anything"/>
</apikit:mapping>
</apikit:mapping-exception-strategy>
这是因为它仅在异常类型 org.mule.module.http.internal.request.ResponseValidatorException 上分支吗?我以为它是为了在状态代码上分支?
还有另一种策略,choice-exception-strategy,它应该在不同的异常对象类型上分支。
APIkit 根据定义的异常值匹配异常,而不是根据定义的statusCode。使用选择异常策略并在其中定义多个捕获异常策略。确保每个捕获异常策略都匹配一个唯一的异常,以便为正确的 http.status 代码和所需的异常负载铺平道路。
例如,如果要抛出 400,请确保捕获异常策略与 BadrequestException 匹配。如果状态代码来自 HTTP 请求者,则匹配 ResponseValidatorException 并设置传入 http.status 和异常负载
基于 http.status 的异常分支可以使用选择异常策略定义,如下例所示,
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy when="#[message.inboundProperties.'http.status'=='404']" doc:name="Catch Exception Strategy" >
<logger message="Exceptions message is ... #[exception.message]" level="ERROR" doc:name="exceptionLogger"/>
<set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
<set-payload value="{ errors: { errorCode: #[message.inboundProperties.'http.status'], errorMessage: #[flowVars.exceptionMessage] } }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>
<catch-exception-strategy doc:name="Catch Exception Strategy" >
<logger message="Exception message is ... #[exception.message]" level="ERROR" category="com.project.stacktrace" doc:name="exceptionLogger"/>
<set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
<set-payload value="{ errors: { errorCode: #[message.inboundProperties.'http.status'], errorMessage: #[flowVars.exceptionMessage] } }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>
</choice-exception-strategy>