Error Handling in Windmill
There are 5 ways to do error handling in Windmill.
try/catch inside a Script
One way to handle errors in Windmill is by using the try/catch
block within a Script. This method is not specific to Windmill and can be used in any programming language that supports exception handling.
Here is an example in TypeScript:
// Define the main function which will handle errors using try/catch
export async function main() {
try {
// Your code that might throw errors goes here
// For example, let's simulate a fetch request
const response = await fetch('https://api.example.com/data');
const data = await response.json();
// Return the result if everything goes well
return data;
} catch (error) {
// Handle errors that might occur during the fetch operation
console.error('An error occurred:', error);
// Return a custom error object or message
return { error: 'An error occurred while fetching data.' };
}
}
Flows' Error Handlers
The Error Handler is a special flow step that is executed when an error occurs within a flow.
If defined, the error handler will take as input the result of the step that errored (which has its error in the 'error field').
Special Case: Error Handling in Flows
There are other tricks to do Error handling in flows, see:
Schedules Error Handlers
Add a special script or flow to execute in case of an error in your scheduled script or flow.
Schedule Error hander is an Enterprise Edition feature.
You can pick the Slack pre-set schedule error handler or define your own.
Workspace Error Handler
Define a script or flow to be executed automatically in case of error in the workspace (e.g. a scheduled job fails to re-schedule).
Workspace Error Handler on Slack
On Cloud plans and Self-Hosted & Enterprise Edition, you can connect workspace to Slack and enable an automated error handler on a given channel.
Custom Workspace Error Handler
You can also define a custom script or flow to be executed automatically in case of error in the workspace.
From the workspace settings, on the "Error Handler" tab and pick a script or flow.
The following args will be passed to the error handler:
- path: The path of the script or flow that errored.
- email: The email of the user who ran the script or flow that errored.
- error: The error details.
- job_id: The job id.
- is_flow: Whether the error comes from a flow.
- workspace_id: The workspace id of the failed script or flow.
The Error handler will be executed by the automatically created group g/error_handler. If your error handler requires variables or resources, you need to add them to the group.
Here is a template for your workspace error handler:
// Workspace error handler template
export async function main(
path: string, // The path of the script or flow that errored
email: string, // The email of the user who ran the script or flow that errored
error: object, // The error details
job_id: string, // The job id
is_flow: boolean, // Whether the error comes from a flow
workspace_id: string // The workspace id of the failed script or flow
) {
const run_type = is_flow ? 'flow' : 'script';
console.log(
`An error occurred with ${run_type} ${path} run by ${email} in workspace ${workspace_id}`
);
console.log(error);
return error;
}
Instance Error Handler
You can define a script to be executed automatically in case of error in your instance (all workspaces).
This Superadmin Error handler is defined by setting the path to the script to be executed as an env variable to all servers using: GLOBAL_ERROR_HANDLER_PATH_IN_ADMINS_WORKSPACE
.
The following args will be passed to the error handler:
- path: The path of the script or flow that errored.
- email: The email of the user who ran the script or flow that errored.
- error: The error details.
- job_id: The job id.
- is_flow: Whether the error comes from a flow.
- workspace_id: The workspace id of the failed script or flow.
Here is a template for your workspace error handler:
// Global / workspace error handler template
export async function main(
path: string, // The path of the script or flow that errored
email: string, // The email of the user who ran the script or flow that errored
error: object, // The error details
job_id: string, // The job id
is_flow: boolean, // Whether the error comes from a flow
workspace_id: string // The workspace id of the failed script or flow
) {
const run_type = is_flow ? 'flow' : 'script';
console.log(
`An error occurred with ${run_type} ${path} run by ${email} in workspace ${workspace_id}`
);
console.log(error);
return error;
}