Search code examples
jsonxmlmuleanypoint-studiodataweave

Type mismatch error returned due to different type of messages being returned in Mule


I have a Mule workflow that can either receive a response message or an business error message as input into the transform message connector.

When it moves from the transform message connector to object to string it hits this error:

ERROR 2016-10-07 18:08:40,187 [[userprocess].userprocess-httpListenerConfig.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Exception while executing: 
    }) when ((payload[0].userResponse != null and payload[0].userResponse.category == "SUCCESS")) and
                         ^
Type mismatch
     found :name, :string
  required :name, :object
Type                  : com.mulesoft.weave.mule.exception.WeaveExecutionException
Code                  : MULE_ERROR--2
********************************************************************************
Exception stack is:
1. Type mismatch
     found :name, :string
  required :name, :object (com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchException)
  com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchNode:65 (null)
2. Exception while executing: 
    }) when ((payload[0].userResponse != null and payload[0].userResponse.category == "SUCCESS")) and
                         ^
Type mismatch
     found :name, :string
  required :name, :object (com.mulesoft.weave.mule.exception.WeaveExecutionException)
  com.mulesoft.weave.mule.WeaveMessageProcessor$WeaveOutputHandler:166 (null)
********************************************************************************
Root Exception stack trace:
com.mulesoft.weave.engine.ast.dynamic.DynamicDispatchException: Type mismatch
     found :name, :string
  required :name, :object

I think it is because when the message is processed by transform message it performs logic based on different fields so what I need to do to fix this is to ignore any response message logic if a business error message is returned and vice versa.

How can I do this?

Dataweave code (JSON to XML):

%dw 1.0
%output application/xml
---
{
    (Data: {
        userId: flowVars.queryParams.userId,
        Message: "User created successfully"
    }) when (payload[0].userResponse.category == "SUCCESS") and
            (payload[1].userResponse.category == "SUCCESS"),            

    (Exception: {
        userId: flowVars.queryParams.userId,
        Message: payload[1].exception.message
        }) when (payload.exception?) and 
                (payload[1].exception.action == "createUser") and
                (payload[0].exception.code != "-1"),
    (Exception: {
       userId: flowVars.queryParams.userId,
       Message: payload[0].exception.message
       }) when (payload.exception?) and 
            (payload[0].exception.action == "amendUser") and
            (payload[1].exception.replyStatus.code != "-1"),
}

Solution

  • You can use when/otherwise in this situation. You were close, just use { and } instead of parens. Something along the lines of:

    %dw 1.0
    %input payload application/json
    %output application/xml
    ---
    {
        Data: {
            userId: flowVars.queryParams.userId,
            Message: "User created successfully"
        }
    }
    when (payload[0].userResponse.category == "SUCCESS") and
         (payload[1].userResponse.category == "SUCCESS"))
    otherwise
    {
        Exception: {
            userId: flowVars.queryParams.userId,
            Message: payload[1].exception.message
        }
    }