https://github.com/aarondill/never-throw
Simplify error handling!
https://github.com/aarondill/never-throw
error error-handling javascript js neverthrow ts typescript
Last synced: about 2 months ago
JSON representation
Simplify error handling!
- Host: GitHub
- URL: https://github.com/aarondill/never-throw
- Owner: aarondill
- License: mit
- Created: 2023-01-04T17:03:38.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-10T15:04:25.000Z (over 3 years ago)
- Last Synced: 2025-09-08T19:39:17.146Z (10 months ago)
- Topics: error, error-handling, javascript, js, neverthrow, ts, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@aarond309/never-throw
- Size: 463 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: License.txt
Awesome Lists containing this project
README
# neverThrow.js
Simplify error handling!
## Usage:
import the named functions
(The neverThrow function can also be imported through the default import):
```javascript
import { neverThrow, neverThrowAsync } from "@aarondill/never-throw";
```
Call `foo` with error handling
```js
const result = neverThrow(foo);
// Pass arguments to foo:
const result = neverThrow(foo, 1, 2, 3);
// Using an async function:
const result = await neverThrowAsync(bar, 1, 2, 3);
// ^ Returns a promise
```
Returns an object with 'return' or 'error' depending on if the function threw.
Example output:
```js
console.log(neverThrow(x=>x, "Hello, world!"));
{
isErr: false,
return: "Hello, world!",
args: ["Hello, world!"],
function: x=>x,
}
```
If an error is thrown:
```js
// Throws a string!!
console.log(neverThrow(function(x){throw x}, "Hello, world!"));
{
isErr: true,
err: Error("Hello, world!"),
args: ["Hello, world!"],
function: function(x){throw x},
}
```
## Notes:
- If no value is returned, 'return' will be `undefined`
- `function` is the same function as was passed in
- Any non-error value thrown will be converted to an error by `Error(String(thrownError))`
- No mutation will be done on any value passed in.
## Typescript
### Exported Types:
- BaseReturn - The return value, without 'err' or 'success' properties
- Err - The object return when an error is thrown
- Success - The object returned when no error is thrown
- AwaitedSuccess - Success returned from `neverThrowAsync`, equivalent to Success
- AwaitedErr - Error returned from `neverThrowAsync`, equivalent to Err
### Note:
```typescript
if (result.isErr === true) result is Success
else result is Err
```