Let’s suppose that you have an Interlok instance servicing HTTP requests and the data being transferred around is JSON messages. In the event that an exception happens what would normally happen is an exception is printed in the log file and a HTTP 500 error returned back to the client. What if we want to send more information such as the stack trace back to the caller as a JSON message.
When an exception is thrown during processing; then 3 things happen
- The exception is stored as part of object metadata (against the key
- The location of the exception is stored as part of object metadata (against the key
- The workflow where the exception happened is recorded as normal metadata against the key
We can use EmbeddedScriptingService to build up an exception report that can be rendered as JSON by the Jackson JSON streaming API classes1. If you are already depending on the adp-json optional package then you’ll already have these classes available to you.
So our exception handling chain becomes :
- Grab the object metadata.
- Create a map to store our report and store the workflowId in it.
- If an exception exists in object metadata, then store the exception itself, and the exception message into the report
- If the cause exists in object metadata, then store the cause in the report
- Render the map as text using the jackson ObjectMapper.
- Send the payload back to the caller.
With an example JSON response of :