Swiftybase
Function StackCustom Function

Catch Exception

Intercepts and handles exceptions that occur within your function stack, allowing you to implement custom error handling logic.

Purpose

Use Catch Exception to gracefully handle errors in your function stack instead of letting them crash your application. This function gives you control over error processing, enabling you to:

  • Log error details
  • Transform errors into user-friendly messages
  • Return fallback values
  • Conditionally re-throw exceptions
  • Implement recovery logic

Config

The Catch Exception function must be placed:

  • As the first function in the root stack, or
  • At the beginning of any function that creates a child stack

Within the catch handler, you'll define custom logic for processing errors. You can choose to:

  • Return a modified result to allow processing to continue
  • Log the error and re-throw it
  • Transform the error into a different format

Example: Implementing Exception Handling

Add Catch Exception

Add the Catch Exception function at the root of your function stack and Click "Define Catch Stack" to create a custom stack specifically for handling errors.

Implement Error Processing

In this example, we log the error by passing the error variable to a Print function. The error variable automatically contains information about the exception that was thrown.

Return Response (Optional)

You can choose to return the API response directly with the error information. Alternatively, based on your requirements, you can:

  • Re-throw the exception
  • Modify the data and return appropriate values to the caller
  • Return fallback data

After configuring your error handling logic, use the close icon to return to the main stack and save your changes.

Test Your Error Handling

Run your function to verify that it correctly handles exceptions and returns the expected result.

To get the most out of exception handling, pair this with the Throw Exception function to deliberately trigger errors when specific conditions are met. See the Throw Exception documentation.

Best Practices

  • Always include error handling in production functions
  • Be specific about which errors you want to catch
  • Include meaningful error messages and context
  • Consider logging errors for later troubleshooting
  • Only catch errors you can actually handle
  • Don't silently swallow errors without logging
  • Return consistent error formats for better client handling

On this page