try

Learn how to handle errors in template execution.

This action wraps actions with error handling, similar to a try/catch/finally block.

If the action fails, the else branch runs as a fallback. The finally branch always runs regardless of the outcome. When there is no else branch, use help to enrich the error message with suggestions and links.

Example

The following template attempts an action that always fails and handles the error gracefully:

template.json5
{  "$schema": "https://schema.croct.com/json/v1/template.json",  "title": "Error handling",  "description": "Demonstrates error handling with try.",  "actions": [    {      "name": "try",      "action": {        "name": "fail",        "message": "Something went wrong."      },      "else": {        "name": "print",        "semantics": "warning",        "message": "The action failed, but we recovered."      },      "finally": {        "name": "print",        "message": "Done."      }    }  ]}

To apply this template, run:

npx croct@latest use template.json5

Custom help

When there is no else branch, use help to provide context for the error:

template.json5
{  "$schema": "https://schema.croct.com/json/v1/template.json",  "title": "Dependency checker",  "description": "Checks for required dependencies.",  "actions": [    {      "name": "try",      "action": {        "name": "check-dependency",        "dependencies": [          {"name": "next"}        ]      },      "help": {        "message": "This template requires a Next.js project.",        "suggestions": [          "Create a new Next.js project and try again."        ],        "links": [          {            "label": "Next.js docs",            "url": "https://nextjs.org/docs"          }        ]      }    }  ]}

If the dependency check fails, the custom message, suggestions, and links are displayed instead of the raw error.

Properties

These are the supported properties:

name
string

The action identifier. Always try for this action.

action
Action|Array<Action>

The actions to attempt.

Accepts a single action or a list of actions.

else(optional)
Action|Array<Action>

The fallback actions to execute if the primary action fails.

Accepts a single action or a list of actions. When provided, help is ignored.

finally(optional)
Action|Array<Action>

The actions to execute after completion, regardless of whether the primary action succeeded or failed.

Accepts a single action or a list of actions. This branch runs even if else also fails.

help(optional)
object

Custom error context displayed when the action fails and no else branch is provided.

If the action fails and neither else nor help is set, the original error is thrown as-is.

message(optional)
string

A custom error message replacing the original one.

suggestions(optional)
Array<string>

A list of suggestions to help resolve the issue.

A list of links with additional information.