4.6.4. 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:

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 the run's output. The value of errors is an array of error objects.

Then, you can check the items in the errors array and handle them accordingly. Each error object has an error property, which holds the name or the text of the thrown error.

Was this chapter helpful?