Try Catch and Raise Error

Overview

The Raise expression gives you a sharp tool to flag errors explicitly and stop execution of compound actions. The Try Catch expression complements it by attempting risky code and rerouting on failure.

Raise

This expression triggers an immediate exit, much like throwing an exception in code, but with a stored output for traceability. Always set an output_key to capture the error object (status, details), and add a message for clarity in logs or chats.

Try Catch

The try_catch expression allows for the execution of a compound action expression with an error handling mechanism. If the try block encounters an error, the catch block is executed, allowing for graceful error handling and recovery. This is particularly useful for managing errors in compound actions where certain actions might fail under known or unknown conditions.

FeatureRaise ErrorTry Catch
Primary RoleIntentional failure triggerFailure containment and route
Best Paired WithConditionals like switch or catch blocksUnreliable actions

Low-Code Editor

Combine the Try Catch and Raise steps for graceful error handling

  1. Set Up Your Steps: Accordingly set up steps within your Try and Catch blocks
  1. Filter by Status Code: For common errors and expected issues, set the catch block to trigger on specific status codes.
  1. Add Graceful Exit: Add a Raise step to provide an error message and gracefully end the execution of the flow.

Syntax Reference

Raise Schema

raise:
  output_key: ERROR_VAR*  # any: Holds error details (e.g., {status: E403, message: ...})
  message: 'ERROR_MSG'    # str: Optional log/chat note
FieldTypeMandatoryDescription
output_keystringYesError storage for data bank
messagestringNoMustache expression string, e.g., "There was an error {{ error }} "

Try Catch Schema

try_catch:
  try:
    steps:  # Expressions to attempt
      - EXPRESSION_1  # e.g., flaky action
  catch:
    on_status_code: [404, 500]  # Optional
    steps:  # Recovery (no error details available)
      - EXPRESSION_2  # e.g., notify with static msg
FieldTypeMandatoryDescription
try.stepslist[expr]YesAttempted expressions
catch.on_status_codelist[str]NoError filters; omit for all
catch.stepslist[expr]YesRecovery expressions

Practical Examples

Raise

Example 1: Permission Gate

Halt non-admins

steps:
  - switch:
      cases:
        - condition: meta_info.user.role != 'admin'
          steps:
            - raise:
                output_key: auth_error
                message: "Access denied: Admin required"
  - action:
      name: perform_admin_task
      output_key: admin_task_result

Try Catch

Example 1: Action Recovery

Catch a 404, notify the admin and raise the error

try_catch:
  try:
    steps:
      - action:
          action_name: may_fail_action
          output_key: action_result
  catch:
    on_status_code: [404]
    steps:
      - action:
          action_name: notify_admin
          output_key: admin_alert
          input_args:
            message: "API returned 404—standard flake"
      - raise:
          output_key: user_error
          message: "Item not found—try a different query"

Example 2: General Fallback

Log any error and exit

try_catch:
  try:
    steps:
      - action:
          action_name: external_call
          output_key: api_data
  catch:  # Catches all
    steps:
      - raise:
          output_key: service_down
          message: "Service temporarily down—retry later"