Notes: Module 8: Mule 3 Fundamentals: Handling Errors

Types of exceptions:
- System exceptions (thrown at system-level with no message is involved)
- Message exceptions (thrown within a flow whenever a message is involved)

When a system exception occurs, a system exception strategy is invoked:
- Non-configurable
- Logs the exception
- Exception caused by a connection failure executes the reconnection strategy

8-1) Handling messaging exceptions

The ‘default exception strategy’ is used if there is no exception strategy explicitly defined.

The ‘default exception strategy’:
- Implicitly and globally handles all message exceptions thrown in Mule applications.
- Stops execution of the flow and logs the exception.
- Cannot be configured but can be replaced with your own global exception strategy.

Image: Exception makes normal flow stop ---> 1st message processor in the exception strategy

Defining message exception strategies:
- Add to error handling section of flow.
- Each flow can contain only one (choice exception strategies can contain one or more catch and/or rollback)
- Can contain any number of message processors.

Image: Referencing the exception inside the strategy (exception.)

Return status codes:
- Default 200 for HTTP
- Modify with: message.outboundProperties.’http.status’
--- Or use the mapping exception strategy.

Mule Palette: ‘Error Handling’
- Catch Exception Strategy
- Choice Exception Strategy
- Custom Exception Strategy
- Exception Mapping
- Mapping Exception Strategy
- Reference Exception Strategy
- Rollback Exception Strategy

Walkthrough 8-1: Handle a messaging exception
- Add a catch exception strategy to a flow.
- Catch the exception and set the payload to send an error message back.
- Reference the exception object inside an exception handler.
- Set an HTTP status code inside the exception hander.

Drag the ‘Catch Exception Strategy’ into the flow.

Image: Configuring a Set Payload inside the Catch Exception Strategy

Image: Catch Exception Strategy containing Set Payload, Property and Logger: We don’t want 200 OK returned so we use Property to set the HTTP return value to 400.

8-2) Handling different types of exceptions

The Choice Exception Strategy

Execute When:

Bubbling Exceptions
“If a flow does not have an exception strategy, the strategy of the calling flow is used.”

Walkthrough 8-2: Handle different types of messaging exceptions
- Add and configure a choice exception strategy.
- Get exceptions handled by both of the catch exception strategies in the choice strategy.
- Create a new flow that calls a flow that has an exception so the exception can bubble up and be handled by the calling flow.

‘Data unavailable - all other exception’ Catch Exception Strategy:
Set Payload value:
DATA IS UNAVAILABLE. TRY LATER. '['\n' + exception]

‘No flights - DW exception’ Catch Exception Strategy:
Execute When:
#[exception.causeMatches('com.mulesoft.weave.* ')]

Image: Choice Exception Strategy

8-3) Defining global exception strategies

- You can reuse exception handling strategies by defining them outside a flow.
--- Drag them out and drop outside any flow.
--- Typically, put them in your global configuration file.
- Reference global exception handlers using the ‘Reference Exception Strategy’

Walkthrough 8-3: Create and use global exception strategies
- Create a global exception handler
- Reference and use the global exception handler in flows

Image: Create and use global exception strategies

“Items in global.xml have a ‘name=’

8-4) Defining a Default Exception Strategy

The ‘Default Exception Strategy’ can be replaced with your own global default exception strategy:
1: Create a global configuration element.
2: Specify a default exception strategy in the global configuration element.

Walkthrough 8-4: Specify a global default exception strategy
- Create a global configuration element in the global.xml file.
- Specify a default exception strategy in the global configuration element.
- Remove the existing exception handling strategies.
- Use the default exception handling strategy.

Image: Global Element Properties: Default Exception Strategy

Then, if you were using a ‘Reference Exception Strategy’ you can delete that.

8-5) Review a mapping exception strategy

Walkthrough 8-5: Review a mapping exception strategy
- Locate the mapping exception strategy that was created automatically by APIkit for the interface for the American flights web service.