Error Handling in a Shortcut

One aspect of shortcuts that can be frustrating is error handling. Often, when an error occurs, the shortcut simply reports an error and stops. In the following example, the List action contains two items and the Get Item from List action attempts to get item three:

The Shortcuts app does not have anything similar to AppleScript’s try statement. So, when possible, the best approach is to prevent the error from happening:

Another alternative is to place the actions that may cause an error in a separate shortcut and to call that shortcut by way of an AppleScript with a try statement. There’s many ways this could be implemented and the following is a simple one:

There are two additional options. One is to use JavaScript to catch the error–I’m not familiar with JavaScript and can’t give an example of this. The second is to use a x-callback-url (documentation here). This seems useful, but, unfortunately, it loads the Shortcuts app whenever it is used.

The Shortcuts app’s documentation (here) contains the following:

Occasionally, a running shortcut may encounter an error or other issue. For example, an action that grabs the current Wi-Fi network name may grab a network that’s not your home network. If you expect that an action may create an issue, you can use the Stop Shortcut action or the Stop and Output action inside of an If action to halt the shortcut if a specific condition occurs. Stop actions are similar to a return or break statement in Swift.

This sounds great, but it doesn’t appear to work in some circumstances. Using my original example:

The shortcuts from above:

List.shortcut (21.7 KB)

List 1.shortcut (22.1 KB)

List 2.shortcut (22.1 KB)

List 3.shortcut (21.9 KB)

When an error occurs, the shortcut will often notify the user of the nature of, and the action that caused, the error. If that doesn’t happen, there are a few actions that can be used to step through a shortcut to troubleshoot the error.

  • The Show Result action previews the content of the prior action in a dialog and can be set to show a property of the result of the prior action. The user can then stop or continue the shortcut. This action is quite useful overall and is especially useful when troubleshooting a shortcut called by another shortcut.

  • The Stop and Output action is similar to the Show Result action except that it stops the shortcut and displays the information at the end of the shortcut (which can be cumbersome with a long shortcut).

  • The Quick Look action allows the user to view the contents of a prior action using the Quick Look utility. The Shortcuts documentation makes special note of this action for testing a shortcut, although I have not found it to be particularly useful.

  • The View Content Graph action typically (but not always) shows the result of a prior action and allows the user to view the type of a particular result. It is especially useful when viewing the contents of a list created with the Add to Variable action. For example,

The content item graph for the second list item looks like this: