File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -69,12 +69,13 @@ import type { Result } from "ts-explicit-errors"
69
69
import { attempt , err , isErr } from " ts-explicit-errors"
70
70
71
71
function getConfig(): Result <string > {
72
- const config = attempt (() => fs .readFileSync (" config.json" ))
73
- if (isErr (config )) return err (" failed to read config file" , config )
72
+ const rawConfig = attempt (() => fs .readFileSync (" config.json" ))
73
+ if (isErr (rawConfig )) return err (" failed to read config file" , rawConfig )
74
74
75
- // you would probably parse the config here
75
+ const parsedConfig = attempt (() => JSON .parse (rawConfig ))
76
+ if (isErr (parsedConfig )) return err (" failed to parse config" , parsedConfig )
76
77
77
- return config
78
+ return parsedConfig
78
79
}
79
80
```
80
81
@@ -85,7 +86,9 @@ At some point, you'll want to handle the error chain. Use `messageChain` to log
85
86
86
87
const config = getConfig ()
87
88
if (isErr (config )) {
88
- console .error (config .messageChain ) // failed to read config file -> ENOENT: no such file or directory, open 'config.json'
89
+ console .error (config .messageChain )
90
+ // (if 'fs.readFileSync' failed): failed to read config file -> ENOENT: no such file or directory, open 'config.json'
91
+ // (if 'JSON.parse' failed): failed to parse config -> SyntaxError: JSON Parse error
89
92
process .exit (1 )
90
93
}
91
94
You can’t perform that action at this time.
0 commit comments