New-client launch offer: talk to sales about a focused integration pilot using MuleSoft or n8n. See what's included →
Home / Insights / MuleSoft Choice: handling unmatched conditions
MuleSoft

MuleSoft Choice: handling unmatched conditions

Learn why an otherwise route matters and how to handle messages that do not match your routing conditions.

Choice is the simplest router in Mule and the one that causes the quietest failures. It evaluates each when in order, takes the first that returns true, and skips the rest. One route runs. Never two.

The silent pass-through

If nothing matches and there is no otherwise, the message is not rejected. It is not logged. It simply continues past the router carrying whatever payload it arrived with, and the next processor treats it as normal traffic.

Six months later someone adds a new document type upstream, it matches none of your conditions, and it flows through the happy path as though it had been handled. The bug does not surface until the data is wrong somewhere else entirely.

Always write the otherwise. Even when it is unreachable. Especially when it is unreachable.
<choice doc:name="Route by type">
  <when expression="#[vars.docType == 'INVOICE']">
    <flow-ref name="handle-invoice"/>
  </when>
  <when expression="#[vars.docType == 'CREDIT_NOTE']">
    <flow-ref name="handle-credit-note"/>
  </when>
  <otherwise>
    <!-- not optional -->
    <raise-error type="APP:UNKNOWN_DOC_TYPE"
      description="#['Unrouted docType: ' ++ (vars.docType default 'null')]"/>
  </otherwise>
</choice>

Order is logic

First match wins, so a broad condition placed above a narrow one makes the narrow one dead code. This is easy to introduce during a change and invisible in review unless someone is looking for it. If two conditions can both be true for the same message, the ordering is the business rule — write it down in a comment.

Keep the expression boring

The expression must evaluate to a boolean. Two habits prevent most of the trouble here:

  • Decide before you route. Compute the routing key into a variable in a Transform above the Choice, then compare the variable. The router stays readable and the value is visible in the debugger and the logs.
  • Handle null explicitly. vars.docType == 'INVOICE' is false when the variable is null, which is usually right — but payload.header.type will throw if header is missing. Use default or the safe navigation you would use anywhere else.

Choice or Scatter-Gather?

They look similar in the canvas and do opposite things. Choice picks one route out of several. Scatter-Gather runs all of them. If you find yourself writing several Choice routers in a row to fire off multiple calls, you wanted Scatter-Gather.

Building something where this matters?

Discuss a focused integration pilot covering the agreed workflow, error handling, testing and handover. Talk to our team about available new-client launch benefits.

See the scope

More on this

Let’s Talk

Your request has been accepted

Thank you for contacting BraidArc. We typically respond within one business day.

Reference: