How to print exception payload within mule message flow - mule

I have a mule flow within which I am logging the entire payload in String format by following code snippet
<logger level="ERROR" message="#[payload:java.lang.String]"/>
Now if the error occurs there really is no need to print the entire payload. The payload object in the message is null and the exception payload is populated with the relevant exception in it.
If I can print just the exception payload in String format, that would suffice. Does any one know how to log the exception payload from the message ?

<logger level="ERROR" message="
#[groovy:message.getExceptionPayload().getRootException().getMessage()]" />
The above code extracts the message related to the cause of exception.

Quite simply:
<logger level="ERROR" message="#[exception]"/>

small correction in the expression use:
[groovy:message.getExceptionPayload().getRootException().getMessage()]

If you want to save error into payload you need to transform it:
%dw 2.0
output application/json
---
{
error: error.detailedDescription,
errorType: (error.errorType.namespace default '') ++ ":" ++ (error.errorType.identifier default '') ,
recoverable: false
}
So, into payload you have your error

Related

Mule logger throws UnableToInferOutputTypeException

I'm using a Mule 4 logger component in a flow to log two flow variables, var1 and var2. Both vars are strings.
<logger level="INFO" doc:name="Logger" message='#["var1 = $(vars.var1), var2 = $(vars.var2)"]'/>
At runtime, this is throwing an exception:
Message : "org.mule.weave.v2.el.exceptions.UnableToInferOutputTypeException: Unable to infer a output media type as more than one is being used: application/java,application/json please specify using: 'output <your mime-type> --- <expr>'
This is caused by the two vars having different MIME types – one is application/java and the other is application/json. The fix is to explicitly set the output type in the message:
<logger level="INFO" doc:name="Logger" message='#[output application/java --- "var1 = $(vars.var1), var2 = $(vars.var2)"]'/>

How to retrieve elements in foreach loop in Mule4?

I have defined a variable to keep application/json collection.
My original payload contain following json block;
"amendments": {
"amendmentId": ["8a9a84b76b5a5a59016b687bae35012e",
"8a9a84b76b5a5a59016b6888e90e0144"]
}
<set-variable value="#[payload..amendmentId[1]]" doc:name="AmendmentIds" doc:id="03b6c46a-fc7b-43d4-b23a-502146ef0b13" variableName="amendmentids"/>
<logger level="INFO" doc:name="Logger" doc:id="6a1ad892-65f9-4dd2-9891-bdf3ad64c908" message="#[vars.amendmentids]" />
It prints as;
[
"8a9a84b76b5a5a59016b687bae35012e",
"8a9a84b76b5a5a59016b6888e90e0144"
]
Then I use ForEach loop to define few process logic for each Id,
Within foreach loop how I can get, each above id?
I used,vars.amendmentids[vars.counter]], but getting following error[1]
<foreach doc:name="For Each" doc:id="7b741315-aa28-4704-82f0-629c21d93853" collection="#[vars.amendmentids]">
<logger level="INFO" doc:name="Logger" doc:id="049c3137-2f92-4077-8e3f-b26516cc5528" message="#[vars.amendmentids[vars.counter]]"/>
</foreach>
[1]
Message : "Internal execution exception while executing the script, this is most probably a bug, file an issue with the script and the input data.
Caused by:
java.lang.RuntimeException: Unable to infer a output media type as more than one is being used: application/json,application/java please specify using: output <your mime-type> --- <expr>
at org.mule.weave.v2.el.MuleDataWeaveHelper$.inferImplicitOutput(MuleDataWeaveHelper.scala:69)
.............
" evaluating expression: "vars.amendmentids[vars.counter]".
Error type : MULE:EXPRESSION
I fixed it by defining output mime type like;
<logger level="INFO" doc:name="Logger" doc:id="049c3137-2f92-4077-8e3f-b26516cc5528" message="#[output application/java --- vars.amendmentids[vars.counter - 1]]"/>

Mule Soft Data Weave

I want to write a Data Weave Code wherein if the data is null then it should route to 400. How do I write this in Mule Soft?
My flow is as follows:
HTTP -->Transfomer-->Logger
Tranformer DW code
{
event_ops_type: payload.EDM_generic_consumer_message.event_meta_data.event_operation_type
}
Now what I want to implement is if "event_ops_type" is null then route to 400(Exception Handling)?
You may want to try using a validation module. MuleSoft documentation here.
<validation:is-not-null message="event_ops_type is null!" value="#[flowVars.event_ops_type]" exceptionClass="com.example.MyException" doc:name="Validation"/>
You can also use a Groovy script in a choice block to throw any exception you'd like. Here, it will actually throw a 404 with the API generated exception handling. You could switch this to any exception you'd like.
<choice doc:name="Choice">
<when expression="#[flowVars.event_ops_type != null]">
<logger message="#[flowVars.event_ops_type]" level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[throw new org.mule.module.apikit.exception.NotFoundException(flowVars['event_ops_type'] + " is null!"); ]]></scripting:script>
</scripting:component>
</otherwise>
</choice>
<exception-strategy ref="api-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
You can use choice router after transformer to check payload.event_ops_type == "400". Then either raise custom exception for exception handling or set response status and reason based on event_ops_type 400.

Not able to access Jira Payload in JAVA (java.util.Arrays$ArrayList)

Here is my Flow information. Trying to GET one issue from JIRA and want to setup that issueID to another project. Here i want to use Custom Transformer and setup all objects using JAVA coding.
<jira:config name="Jira" connectionUser="XXXXXXX" connectionPassword="XXXXX" connectionAddress="http://XXXXXXX/rpc/soap/jirasoapservice-v2" doc:name="Jira">
<jira:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/>
<reconnect count="3"/>
</jira:config>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="jira-pocFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/input" doc:name="HTTP"/>
<jira:get-issues-from-jql-search config-ref="Jira" jqlSearch="id=MRT-75" maxNumResults="100" doc:name="Jira"/>
<set-variable variableName="payload" value="#[payload[0]]" doc:name="Variable"/>
<component class="JIRATransformer" doc:name="Java"/>
<jira:create-issue-using-object config-ref="Jira" doc:name="Jira" >
<jira:issue ref="#[message.payload]"/>
</jira:create-issue-using-object>
<logger level="INFO" doc:name="Logger"/>
</flow>
I am trying to access JIRA payload object but it's throwing me Error as Type Cast Exception.
#Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws org.mule.api.transformer.TransformerException {
ArrayList<com.atlassian.jira.rpc.soap.beans.RemoteIssue> list = new ArrayList(Arrays.asList(message.getPayload()));
for(RemoteIssue q : (List<RemoteIssue>) list){
System.out.println("Print AssigneeInfo:->"+q.getAssignee());
}
}
I am getting following Errors.
ERROR 2015-04-15 19:58:59,526 [[jira-poc].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Component that caused exception is: DefaultJavaComponent{jira-pocFlow.component.887693985}. Message payload is of type: Arrays$ArrayList
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. java.util.Arrays$ArrayList cannot be cast to com.atlassian.jira.rpc.soap.beans.RemoteIssue (java.lang.ClassCastException)
JIRATransformer:29 (null)
2. Component that caused exception is: DefaultJavaComponent{jira-pocFlow.component.887693985}. Message payload is of type: Arrays$ArrayList (org.mule.component.ComponentException)
org.mule.component.DefaultComponentLifecycleAdapter:348 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to com.atlassian.jira.rpc.soap.beans.RemoteIssue
at JIRATransformer.transformMessage(JIRATransformer.java:29)
at org.mule.transformer.AbstractMessageTransformer.transform(AbstractMessageTransformer.java:141)
at org.mule.transformer.AbstractMessageTransformer.transform(AbstractMessageTransformer.java:69)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
I tried to follow this Documentation URL but couldn't able to figure it out.
http://mulesoft.github.io/jira-connector/java/com/atlassian/jira/rpc/soap/beans/RemoteIssue.html
I want to Access this Payload and want to update some details from payload object here. I can access payload using MEL expression #[payload[0]] and it automatically coverts it to com.atlassian.jira.rpc.soap.beans.RemoteIssue but using Java code i am not able to type cast it.
Can you please help me to cast this object correctly so i can access the Payload here ?
Thanks.
There's a bug in your component.
This Arrays.asList(message.getPayload()) wraps the message payload into a list. But the message payload is already a List<RemoteIssues> so this wrapping is unnecessary.
If you look at the source code of the JIRA connector, you'll see that MuleSoft prefers late casting of the RemoteIssue. I suggest you do the same:
for (Object o : ((List) message.getPayload())) {
RemoteIssue ri = (RemoteIssue) o;
...
}

Setting Map as payload in Mule Flow using MEL

I am trying to generate and set a map (with 2 key value pairs) on the fly as the payload for the following HTTP call. However, the MEL expression for creating the Map is not working.
<sub-flow name="call-myservice" doc:name="call-myservice">
<set-payload value="#[username :${my.username}, password : ${my.password}]" doc:name="Set Payload"/>
<https:outbound-endpoint exchange-pattern="request-response" host="${myservice.Host}" method="POST" mimeType="application/json" doc:name="My Service call" path="mypath" port="443"/>
</sub-flow>
I followed the instructions at http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+MEL
which suggests --
MEL provides a streamlined way to access map data.
Rather than constructing a map with a new statement, and then using its put method to populate it, you can simply write the following:
[key1 : value1, key2 : value2, . . .]
However, it is giving me the following exception --
ERROR 2014-02-28 15:27:51,424 [[services-proxy].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Execution of the expression "username :abc, password : pwd" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. [Error: unresolvable property or identifier: username]
[Near : {... username :abc, pa ....}]
^
[Line: 1, Column: 1] (org.mvel2.PropertyAccessException)
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer:687 (null)
You are missing square brackets to delimit the map (the only ones you have delimit the Mule expression). Change it to:
<set-payload value="#[['username' :${my.username}, 'password' : ${my.password}]]" doc:name="Set Payload"/>