You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [`@shopify/cli-kit`](https://www.npmjs.com/package/@shopify/cli-kit) NPM package provides utilities to abstract away interactions with the Shopify platform (e.g., authentication, API requests) and ensures experiences are consistent across the board. If you are creating a new plugin or contributing to an existing one, we recommend checking out the following resources:
27
28
28
-
-[Errors](cli-kit/errors.md)
29
29
-[Creating a new command or flag](cli-kit/command-guidelines.md)
30
30
-[Content and UI guidelines](cli-kit/ui-kit/guidelines.md)
Copy file name to clipboardExpand all lines: docs/cli-kit/ui-kit/readme.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,7 +141,7 @@ All banners (including errors) are rendered with a width of 2/3 of the full widt
141
141
142
142
#### Errors
143
143
144
-
If you're using the `cli-kit`'s `runCLI` function to wrap your CLI, you can throw `AbortError` and let the runner display the exception properly. More on what `AbortError` accepts [here](../errors.md#aborting-the-execution-using-errors).
144
+
If you're using the `cli-kit`'s `runCLI` function to wrap your CLI, you can throw `AbortError` and let the runner display the exception properly. More on what `AbortError` accepts [here](../../cli/error_handling.md#aborterror).
145
145
146
146
If you're using your own custom errors or you're not using `runCLI` then you can use the `renderFatalError` function.
147
147
Make sure that your error extends the `cli-kit` class `FatalError` (`AbortError` already does) and pass it to `renderFatalError`
Copy file name to clipboardExpand all lines: docs/cli/error_handling.md
+55-1Lines changed: 55 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,21 @@
2
2
3
3
* These follow from the [General Principles](https://vault.shopify.io/teams/734/pages/Error-Handling-Principles~zEv1.md) document.
4
4
5
+
## Minimize the likelihood of errors happening
6
+
7
+
**Errors can and will happen**, and handling them gracefully is essential to provide a first-class developer experience.
8
+
9
+
When designing code, you can leverage tools to **minimize the likelihood of errors happening**. For example, you can leverage Typescript to prevent invalid states from entering the system. It requires additional time to figure out how to model the state, but the investment pays off significantly. If an external state enters the system, for example, a project in the file system, **validate it and return early** if the shape is invalid. It is similar to what web servers do with incoming HTTP requests. We also recommend modeling business logic as a combination of pure functions that are predictable. Functions with the freedom to mutate input state might get mutations introduced in the future that breaks the contract with callers of the function.
10
+
11
+
Moreover, you should think about scenarios other than the happy path and build for them. There are infinite of those, especially considering that the CLI runs in environments we don't control, so it's essential that you focus on the most obvious ones and don't get too obsessed with trying to anticipate every possible error. **Excellent error management is an iterative process** using unhandled errors as input to inform the improvements.
12
+
5
13
## Global type and handler for fatal errors
6
14
7
15
You’ll see in the section below that the CLI currently makes an exception to some of the general principles presented in this doc. This may not be true forever since there are some upcoming projects that will change the tradeoffs.
8
16
9
17
### Leverage the `FatalError` hierarchy to short circuit the program and present a custom UX to the user
10
18
11
-
The CLI defines `FatalError` and several subtypes of `FatalError`. Within the CLI codebase, you can raise any of these errors at any time to **fully halt the program and present a specific UX to the user**.
19
+
The CLI defines `FatalError` and several subtypes of `FatalError`. Within the CLI codebase, you can raise any of these subtypes at any time to **fully halt the program and present a specific UX to the user**.
12
20
13
21
**Principle**: choose the right error type if you need to abort the program and handle the situation in a specific way:
14
22
@@ -17,6 +25,8 @@ The CLI defines `FatalError` and several subtypes of `FatalError`. Within the CL
17
25
* Use `AbortSilentError` for user-initiated cancellations
18
26
* Use `ExternalError` when external commands fail
19
27
28
+
Please, **don't** use the global `process.exit` and `process.abort` APIs. Also, don't `try {} catch {}` abort errors. If you need to communicate the failure of an operation to the caller (e.g., a 5xx HTTP response), use the result type from the following section.
29
+
20
30
#### AbortError
21
31
22
32
Represents expected user/environment issues that require user action
@@ -28,6 +38,20 @@ Represents expected user/environment issues that require user action
28
38
* Reported as "expected\_error" to analytics (it’s a user problem, not a Shopify one)
29
39
* Sent to Bugsnag as "handled"
30
40
41
+
You can also pass some next steps to the error constructor to help the user recover from the error. The next steps are displayed after the error message.
42
+
43
+
For example
44
+
```ts
45
+
thrownewAbortError(
46
+
"The project doesn't exist",
47
+
undefined,
48
+
[
49
+
"Make sure the command is executed from a project's directory",
50
+
"Run the command again",
51
+
]
52
+
)
53
+
```
54
+
31
55
#### BugError
32
56
33
57
Represents unexpected issues that indicate CLI bugs
@@ -114,6 +138,36 @@ The `FatalError` pattern will not work well in an architecture where app develop
114
138
115
139
However, until then, we get a lot of leverage in the CLI from `FatalError`, so it can continue to exist as a high leverage counter-example to some of our general principles.
116
140
141
+
## Report a result from a function
142
+
There are scenarios where a function needs to inform the caller about the success or failure of the operation. For that, `@shopify/cli-kit` provides a result utility:
The CLI has a unique pattern for classifying non-fatal errors as environment issues through `shouldReportErrorAsUnexpected` and `errorMessageImpliesEnvironmentIssue`.
0 commit comments