@stacksjs/error-handling 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +131 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +14 -0
- package/package.json +53 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Open Web Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Stacks Error Handling
|
|
2
|
+
|
|
3
|
+
Similar to the way Rust handles errors and other functional programming languages.
|
|
4
|
+
|
|
5
|
+
>Encode failure into your program. This package contains a Result type that represents either success (`Ok`) or failure (`Err`).
|
|
6
|
+
>
|
|
7
|
+
>For asynchronous tasks, this package offers a `ResultAsync` class which wraps a `Promise<Result<T, E>>` and gives you the same level of expressivity and control as a regular `Result<T, E>`.
|
|
8
|
+
>
|
|
9
|
+
>`ResultAsync` is "thenable" meaning it behaves exactly like a native `Promise<Result>`, except you have access to the same methods that Result provides without having to `await` or `.then` the promise. _- neverthrow_
|
|
10
|
+
|
|
11
|
+
Read more about the API in the documentation [here](https://github.com/supermacro/neverthrow).
|
|
12
|
+
|
|
13
|
+
## โ๏ธ Features
|
|
14
|
+
|
|
15
|
+
Currently, a wrapper of the `neverthrow` API.
|
|
16
|
+
|
|
17
|
+
- Type-Safe Errors
|
|
18
|
+
- Encode failure into your program
|
|
19
|
+
|
|
20
|
+
## ๐ค Usage
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm i -D @stacksjs/error-handling
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
You can now use it in your project:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import {
|
|
30
|
+
Err,
|
|
31
|
+
Ok,
|
|
32
|
+
Result,
|
|
33
|
+
ResultAsync,
|
|
34
|
+
err,
|
|
35
|
+
errAsync,
|
|
36
|
+
fromPromise,
|
|
37
|
+
fromSafePromise,
|
|
38
|
+
fromThrowable,
|
|
39
|
+
ok,
|
|
40
|
+
okAsync,
|
|
41
|
+
} from '@stacksjs/error-handling'
|
|
42
|
+
|
|
43
|
+
// ...
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Example #1
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const result = ok({ myData: 'test' }) // instance of `Ok`
|
|
50
|
+
|
|
51
|
+
result.isOk() // true
|
|
52
|
+
result.isErr() // false
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Example #2
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const command = 'rimraf ./pnpm-lock.yaml ./node_modules/ ./.stacks/**/node_modules'
|
|
59
|
+
const result = await runCommand(command, options)
|
|
60
|
+
|
|
61
|
+
if (result.isOk()) {
|
|
62
|
+
log.success('Cleaned up.')
|
|
63
|
+
process.exit(ExitCode.Success)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
log.error(result.error)
|
|
67
|
+
process.exit(ExitCode.FatalError)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Learn more in the [docs](https://github.com/supermacro/neverthrow/wiki).
|
|
71
|
+
|
|
72
|
+
## ๐งช Testing
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pnpm test
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## ๐ค Motivation
|
|
79
|
+
|
|
80
|
+
As from the [HackerNews thread](https://news.ycombinator.com/item?id=26191006) relating to "Where Everything Went Wrong: Error Handling and Error Messages in Rust (2020)":
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
Error handling has been wrong since the beginning, and has continued to be wrong ever since.
|
|
84
|
+
|
|
85
|
+
First, we had error codes. Except these were wrong because people forget all the time to check them.
|
|
86
|
+
|
|
87
|
+
Then we had exceptions, which solved the problem of people forgetting to check by crashing the app.
|
|
88
|
+
|
|
89
|
+
Then the Java team got the bright idea to have checked exceptions, which at first helped to mitigate crashes
|
|
90
|
+
from uncaught exception, but caused an explosion in thrown exception signatures, culminating in "catch Throwable".
|
|
91
|
+
|
|
92
|
+
Back to square one.
|
|
93
|
+
|
|
94
|
+
Then we got multi-return error objects, maybes, panics, and all sorts of bright ideas that fail to understand the basic premises of errors:
|
|
95
|
+
|
|
96
|
+
Any error system that relies upon developer discipline will fail because errors will be missed.
|
|
97
|
+
|
|
98
|
+
Any error system that handles all errors the same way will fail because there are some errors we can ignore,
|
|
99
|
+
and some errors we must not ignore. And what's ignorable/retriable to one project is not ignorable/retriable to another.
|
|
100
|
+
|
|
101
|
+
Attempting to get the complete set of error types that any given call may raise is a fool's errand because of the halting
|
|
102
|
+
problem it eventually invokes. Forcing people to provide such a list results in the Java problem for the same reason.
|
|
103
|
+
|
|
104
|
+
It's a hard problem, which is why no one has solved it yet.
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Quote from user [kstenerud](https://news.ycombinator.com/user?id=kstenerud).
|
|
108
|
+
|
|
109
|
+
## ๐ Changelog
|
|
110
|
+
|
|
111
|
+
Please see our [releases](https://github.com/stacksjs/stacks/releases) page for more information on what has changed recently.
|
|
112
|
+
|
|
113
|
+
## ๐ช๐ผ Contributing
|
|
114
|
+
|
|
115
|
+
Please see [CONTRIBUTING](../../.github/CONTRIBUTING.md) for details.
|
|
116
|
+
|
|
117
|
+
## ๐ Community
|
|
118
|
+
|
|
119
|
+
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
|
|
120
|
+
|
|
121
|
+
[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions)
|
|
122
|
+
|
|
123
|
+
For casual chit-chat with others using this package:
|
|
124
|
+
|
|
125
|
+
[Join the Open Web Discord Server](https://discord.ow3.org)
|
|
126
|
+
|
|
127
|
+
## ๐ License
|
|
128
|
+
|
|
129
|
+
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
130
|
+
|
|
131
|
+
Made with โค๏ธ
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Err,
|
|
3
|
+
Ok,
|
|
4
|
+
Result,
|
|
5
|
+
ResultAsync,
|
|
6
|
+
err,
|
|
7
|
+
errAsync,
|
|
8
|
+
fromPromise,
|
|
9
|
+
fromSafePromise,
|
|
10
|
+
fromThrowable,
|
|
11
|
+
ok,
|
|
12
|
+
okAsync
|
|
13
|
+
} from "neverthrow";
|
|
14
|
+
export { Err, Ok, Result, ResultAsync, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync };
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacksjs/error-handling",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.39.0",
|
|
5
|
+
"packageManager": "pnpm@7.15.0",
|
|
6
|
+
"description": "Type safe error handling.",
|
|
7
|
+
"author": "Chris Breuer",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/.stacks/core/error-handling#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
+
"directory": "./.stacks/core/error-handling"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/stacksjs/stacks/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"errors",
|
|
21
|
+
"error-handling",
|
|
22
|
+
"neverthrow",
|
|
23
|
+
"type safe",
|
|
24
|
+
"stacks"
|
|
25
|
+
],
|
|
26
|
+
"main": "dist/index.mjs",
|
|
27
|
+
"module": "dist/index.mjs",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"contributors": [
|
|
30
|
+
"Chris Breuer <chris@ow3.org>"
|
|
31
|
+
],
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=v18.12.1",
|
|
38
|
+
"pnpm": ">=7.15.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@ow3/eslint-config": "^0.33.10",
|
|
42
|
+
"neverthrow": "^5.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"mkdist": "^0.4.0",
|
|
46
|
+
"typescript": "^4.8.4"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "mkdist -d",
|
|
50
|
+
"dev": "mkdist -d",
|
|
51
|
+
"typecheck": "tsc --noEmit"
|
|
52
|
+
}
|
|
53
|
+
}
|