|
| 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