grip/README.md

142 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2024-10-16 05:02:31 +00:00
# Grip
2024-10-17 05:24:15 +00:00
[A take on Go Style Error Handling In JavaScript](https://dev.to/nesterow/a-take-on-go-style-error-handling-in-javascript-577)
2024-10-16 05:02:31 +00:00
Simplified result/error handling for JavaScript.
Grip always returns a consistent call result ready to be handled.
It makes the control flow similar to that of Golang, but doesn't force you to make additional null checks or create transitional variables to hold error results.
2024-10-17 05:24:15 +00:00
2024-10-17 04:08:22 +00:00
Instead of returning a nullish error, Grip always returns a consistent status object:
2024-10-17 04:06:54 +00:00
```javascript
2024-10-17 22:51:30 +00:00
const [value, status] = grip(callable) // or {value, status}
2024-10-17 08:03:07 +00:00
if (status.of(MySpecificError)) {
2024-10-17 04:06:54 +00:00
// handle specific error
}
2024-10-17 22:51:30 +00:00
2024-10-17 08:03:07 +00:00
if (status.fail()) {
2024-10-17 04:08:22 +00:00
// handle any error
}
2024-10-17 04:06:54 +00:00
```
The call result is better than tuple:
```javascript
const result = grip(callable)
2024-10-17 22:51:30 +00:00
2024-10-17 08:03:07 +00:00
if (result.of(MySpecificError)) {
2024-10-17 04:06:54 +00:00
// handle specific error
}
2024-10-17 22:51:30 +00:00
if (result.fail()) {
// handle any error
}
2024-10-17 04:06:54 +00:00
console.log(result.value) // result[0]
```
Grip also works with Promises, functions that return promises, generators and async generators.
2024-10-16 05:02:31 +00:00
## Install
```bash
2024-10-16 05:21:20 +00:00
bun add github:nesterow/grip # or pnpm
2024-10-16 05:02:31 +00:00
```
## Usage
The `grip` function accepts a function or a promise and returns a result with return value and status.
The result can be hadled as either an object or a tuple.
```javascript
import { grip } from '@nesterow/grip';
```
2024-10-17 22:51:30 +00:00
## Handle result as an object
2024-10-16 05:02:31 +00:00
The result can be handled as an object: `{value, status, Ok(), Fail(), Of(type)}`
```javascript
2024-10-16 05:21:20 +00:00
const res = await grip(
2024-10-16 05:02:31 +00:00
fetch('https://api.example.com')
);
2024-10-17 22:51:30 +00:00
if (res.fail()) {
2024-10-17 03:45:32 +00:00
// handle error
2024-10-16 05:02:31 +00:00
}
2024-10-16 05:21:20 +00:00
const json = await grip(
2024-10-16 05:02:31 +00:00
res.value.json()
);
2024-10-17 22:51:30 +00:00
if (json.of(SyntaxError)) {
2024-10-17 03:45:32 +00:00
// handle parse error
2024-10-16 05:02:31 +00:00
}
```
2024-10-17 22:51:30 +00:00
## Handle result as a tuple
2024-10-16 05:02:31 +00:00
The result can also be received as a tuple if you want to handle errors in Go'ish style:
```javascript
2024-10-16 05:21:20 +00:00
const [res, fetchStatus] = await grip(
2024-10-16 05:02:31 +00:00
fetch('https://api.example.com')
);
2024-10-17 23:05:46 +00:00
2024-10-17 22:51:30 +00:00
if (fetchStatus.fail()) {
2024-10-17 03:45:32 +00:00
// handle error
2024-10-16 05:02:31 +00:00
}
const [json, parseStatus] = await grip(
2024-10-16 05:21:20 +00:00
res.json()
2024-10-16 05:02:31 +00:00
);
2024-10-17 23:05:46 +00:00
2024-10-17 22:51:30 +00:00
if (parseStatus.of(SyntaxError)) {
2024-10-17 03:45:32 +00:00
// handle parse error
2024-10-16 05:02:31 +00:00
}
```
2024-10-17 22:51:30 +00:00
## Handle functions
2024-10-17 03:24:06 +00:00
Grip can also handle functions:
```javascript
const [result, status] = grip(() => "ok");
// result = "ok"
const [result1, status1] = grip(() => {
if (1) throw new Error("error")
});
// result1 = null
// status.Of(Error) = true
```
## Handle generators
Generators can be handled using the `Iter()` method:
```javascript
const res = grip(async function* () {
for (let i = 0; i < 3; i++) {
if (i == 2) throw new Error("2");
yield i;
}
});
2024-10-17 22:51:30 +00:00
for await (let [value, status] of res.iter()) {
if (status.of(Error)) {
2024-10-17 03:24:06 +00:00
// handle error properly
break;
}
// typeof value === "number"
console.log(value)
}
```
2024-10-16 05:02:31 +00:00
## License
2024-10-17 23:05:46 +00:00
2024-10-16 05:02:31 +00:00
MIT