Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 6a11cbe

Browse files
authored
[DOC] on creating and throwing error (#502)
1 parent fe2e40c commit 6a11cbe

File tree

2 files changed

+135
-22
lines changed

2 files changed

+135
-22
lines changed

guides/command-implementation.md

+2-22
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,5 @@ E.g.
115115
3. All error shall be created with the error builder,
116116
https://github.com/CatalystCode/spk/blob/master/src/lib/errorBuilder.ts so
117117
that we can generate the exception chain. In this manner, we can precisely
118-
know the root cause of the problem. Error shall be logged at the end of the
119-
command like this
120-
121-
```
122-
export const execute = async (
123-
config: ConfigYaml,
124-
opts: CommandOptions,
125-
exitFn: (status: number) => Promise<void>
126-
): Promise<void> => {
127-
try {
128-
...;
129-
await exitFn(0);
130-
} catch (err) {
131-
logError(
132-
buildError(errorStatusCode.CMD_EXE_ERR, "infra-scaffold-cmd-failed", err)
133-
);
134-
await exitFn(1);
135-
}
136-
};
137-
```
138-
139-
[Reference](../technical-docs/designs/exceptionHandling.md)
118+
know the root cause of the problem. For more information, refer to
119+
[error handling](./error-handling.md).

guides/error-handling.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Error Handling
2+
3+
## Objectives
4+
5+
1. All errors are trace-able. That's from the error messages, we are able to
6+
figure out the execution path (many times, it is not appropriate to dump
7+
stack traces) and root cause(s).
8+
2. An unique status code for each error so that user understand the error domain
9+
which can be `validation error`, `Azure storage management error`,
10+
`git operations related error`, etc.
11+
3. Allows for localization of error message in future.
12+
13+
## Coding details
14+
15+
### imports
16+
17+
```javascript
18+
import { build as buildError } from ”../lib/errorBuilder";
19+
import { errorStatusCode } from "../lib/errorStatusCode";
20+
```
21+
22+
`src/lib/errorBuilder` is the error builder. `src/lib/errorStatusCode` contains
23+
an enum of error type
24+
25+
### throw
26+
27+
#### case 1
28+
29+
```javascript
30+
throw buildError(errorStatusCode.<type>,
31+
"<error-identifier>");
32+
```
33+
34+
example
35+
36+
```javascript
37+
throw buildError(errorStatusCode.GIT_OPS_ERR, "infra-err-git-clone-failed");
38+
```
39+
40+
and in `src/lib/i18n.json`, we have an entry
41+
42+
```
43+
...
44+
"infra-err-git-clone-failed": "Could not clone the source remote repository. The remote repo might not exist or you did not have the rights to access it",
45+
...
46+
```
47+
48+
#### case 2
49+
50+
where we have placeholder in error message
51+
52+
```javascript
53+
throw buildError(errorStatusCode.<type>, {
54+
errorKey: "<error-identifier>",
55+
values: [sourcePath]
56+
});
57+
```
58+
59+
and in `src/lib/i18n.json`, we have an entry
60+
61+
```
62+
...
63+
"infra-git-source-no-exist": "Source path, {0} did not exist.",
64+
...
65+
```
66+
67+
#### case 3
68+
69+
where we nest an error into current error
70+
71+
```javascript
72+
throw buildError(errorStatusCode.<type>,
73+
"<error-identifier>", err);
74+
```
75+
76+
example
77+
78+
```javascript
79+
try {
80+
<perform some azure API calls>
81+
} catch (err) {
82+
throw buildError(errorStatusCode.GIT_OPS_ERR,
83+
"hld-reconcile-err-helm-add", err);
84+
}
85+
}
86+
```
87+
88+
and in `src/lib/i18n.json`, we have an entry
89+
90+
```
91+
...
92+
hld-reconcile-err-helm-add": "Error adding helm chart",
93+
...
94+
```
95+
96+
### write to log before exiting
97+
98+
This will write the entire error chain to the log. example
99+
100+
```
101+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
102+
import { errorStatusCode } from "../../lib/errorStatusCode";
103+
104+
...
105+
export const execute = async (
106+
opts: CommandOptions,
107+
exitFn: (status: number) => Promise<void>
108+
): Promise<void> => {
109+
try {
110+
...
111+
await exitFn(0);
112+
} catch (err) {
113+
logError(
114+
buildError(
115+
errorStatusCode.CMD_EXE_ERR,
116+
"introspect-create-cmd-failed",
117+
err
118+
)
119+
);
120+
await exitFn(1);
121+
}
122+
};
123+
```
124+
125+
We build a final error with `errorStatusCode.CMD_EXE_ERR`, and include the `err`
126+
object. `err` may also include other errors. And we write this final error to
127+
log with `logError` function.
128+
129+
# Appendix
130+
131+
## Reference
132+
133+
1. https://github.com/CatalystCode/spk/blob/master/technical-docs/designs/exceptionHandling.md

0 commit comments

Comments
 (0)