Documentation

5.8.7. Access Workflow Errors

In this chapter, you’ll learn how to access errors that occur during a workflow’s execution.

How to Access Workflow Errors?#

By default, when an error occurs in a workflow, it throws that error, and the execution stops.

You can configure the workflow to return the errors instead so that you can access and handle them differently.

For example:

src/api/workflows/route.ts
5import myWorkflow from "../../../workflows/hello-world"6
7export async function GET(8  req: MedusaRequest,9  res: MedusaResponse10) {11  const { result, errors } = await myWorkflow(req.scope)12    .run({13	    // ...14      throwOnError: false,15    })16
17  if (errors.length) {18    return res.send({19      errors: errors.map((error) => error.error),20    })21  }22
23  res.send(result)24}

The object passed to the run method accepts a throwOnError property. When disabled, the errors are returned in the errors property of run's output.

The value of errors is an array of error objects. Each object has an error property, whose value is the name or text of the thrown error.

Was this chapter helpful?
Edit this page