neverthrow
Advanced tools
Comparing version 3.1.3 to 3.1.4
@@ -81,2 +81,20 @@ 'use strict'; | ||
var defaultErrorConfig = { | ||
withStackTrace: false, | ||
}; | ||
// Custom error object | ||
// Context / discussion: https://github.com/supermacro/neverthrow/pull/215 | ||
var createNeverThrowError = function (message, result, config) { | ||
if (config === void 0) { config = defaultErrorConfig; } | ||
var data = result.isOk() | ||
? { type: 'Ok', value: result.value } | ||
: { type: 'Err', value: result.error }; | ||
var maybeStack = config.withStackTrace ? new Error().stack : undefined; | ||
return { | ||
data: data, | ||
message: message, | ||
stack: maybeStack, | ||
}; | ||
}; | ||
(function (Result) { | ||
@@ -147,7 +165,7 @@ /** | ||
}; | ||
Ok.prototype._unsafeUnwrap = function () { | ||
Ok.prototype._unsafeUnwrap = function (_) { | ||
return this.value; | ||
}; | ||
Ok.prototype._unsafeUnwrapErr = function () { | ||
throw new Error('Called `_unsafeUnwrapErr` on an Ok'); | ||
Ok.prototype._unsafeUnwrapErr = function (config) { | ||
throw createNeverThrowError('Called `_unsafeUnwrapErr` on an Ok', this, config); | ||
}; | ||
@@ -191,6 +209,6 @@ return Ok; | ||
}; | ||
Err.prototype._unsafeUnwrap = function () { | ||
throw new Error('Called `_unsafeUnwrap` on an Err'); | ||
Err.prototype._unsafeUnwrap = function (config) { | ||
throw createNeverThrowError('Called `_unsafeUnwrap` on an Err', this, config); | ||
}; | ||
Err.prototype._unsafeUnwrapErr = function () { | ||
Err.prototype._unsafeUnwrapErr = function (_) { | ||
return this.error; | ||
@@ -197,0 +215,0 @@ }; |
@@ -0,1 +1,5 @@ | ||
interface ErrorConfig { | ||
withStackTrace: boolean; | ||
} | ||
declare namespace Result { | ||
@@ -26,4 +30,4 @@ /** | ||
match<A>(ok: (t: T) => A, _err: (e: E) => A): A; | ||
_unsafeUnwrap(): T; | ||
_unsafeUnwrapErr(): E; | ||
_unsafeUnwrap(_?: ErrorConfig): T; | ||
_unsafeUnwrapErr(config?: ErrorConfig): E; | ||
} | ||
@@ -42,4 +46,4 @@ declare class Err<T, E> { | ||
match<A>(_ok: (t: T) => A, err: (e: E) => A): A; | ||
_unsafeUnwrap(): T; | ||
_unsafeUnwrapErr(): E; | ||
_unsafeUnwrap(config?: ErrorConfig): T; | ||
_unsafeUnwrapErr(_?: ErrorConfig): E; | ||
} | ||
@@ -46,0 +50,0 @@ |
@@ -77,2 +77,20 @@ /*! ***************************************************************************** | ||
var defaultErrorConfig = { | ||
withStackTrace: false, | ||
}; | ||
// Custom error object | ||
// Context / discussion: https://github.com/supermacro/neverthrow/pull/215 | ||
var createNeverThrowError = function (message, result, config) { | ||
if (config === void 0) { config = defaultErrorConfig; } | ||
var data = result.isOk() | ||
? { type: 'Ok', value: result.value } | ||
: { type: 'Err', value: result.error }; | ||
var maybeStack = config.withStackTrace ? new Error().stack : undefined; | ||
return { | ||
data: data, | ||
message: message, | ||
stack: maybeStack, | ||
}; | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
@@ -145,7 +163,7 @@ var Result; | ||
}; | ||
Ok.prototype._unsafeUnwrap = function () { | ||
Ok.prototype._unsafeUnwrap = function (_) { | ||
return this.value; | ||
}; | ||
Ok.prototype._unsafeUnwrapErr = function () { | ||
throw new Error('Called `_unsafeUnwrapErr` on an Ok'); | ||
Ok.prototype._unsafeUnwrapErr = function (config) { | ||
throw createNeverThrowError('Called `_unsafeUnwrapErr` on an Ok', this, config); | ||
}; | ||
@@ -189,6 +207,6 @@ return Ok; | ||
}; | ||
Err.prototype._unsafeUnwrap = function () { | ||
throw new Error('Called `_unsafeUnwrap` on an Err'); | ||
Err.prototype._unsafeUnwrap = function (config) { | ||
throw createNeverThrowError('Called `_unsafeUnwrap` on an Err', this, config); | ||
}; | ||
Err.prototype._unsafeUnwrapErr = function () { | ||
Err.prototype._unsafeUnwrapErr = function (_) { | ||
return this.error; | ||
@@ -195,0 +213,0 @@ }; |
{ | ||
"name": "neverthrow", | ||
"version": "3.1.3", | ||
"version": "3.1.4", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -27,9 +27,9 @@ "main": "dist/index.cjs.js", | ||
"devDependencies": { | ||
"@babel/core": "7.12.9", | ||
"@babel/preset-env": "7.12.7", | ||
"@babel/core": "7.12.10", | ||
"@babel/preset-env": "7.12.11", | ||
"@babel/preset-typescript": "7.12.7", | ||
"@types/jest": "26.0.19", | ||
"@types/node": "14.14.10", | ||
"@typescript-eslint/eslint-plugin": "4.8.2", | ||
"@typescript-eslint/parser": "4.8.2", | ||
"@types/node": "14.14.16", | ||
"@typescript-eslint/eslint-plugin": "4.11.0", | ||
"@typescript-eslint/parser": "4.11.0", | ||
"babel-jest": "26.6.3", | ||
@@ -42,6 +42,6 @@ "eslint": "7.16.0", | ||
"rollup": "2.35.1", | ||
"rollup-plugin-dts": "2.0.0", | ||
"rollup-plugin-dts": "2.0.1", | ||
"rollup-plugin-typescript2": "0.29.0", | ||
"ts-jest": "26.4.4", | ||
"typescript": "4.1.2" | ||
"typescript": "4.1.3" | ||
}, | ||
@@ -48,0 +48,0 @@ "keywords": [ |
@@ -63,5 +63,5 @@ # NeverThrow 🙅 | ||
- `err` convenience function to create an `Err` variant of `Result` | ||
- `Ok` class for you to construct an `Ok` variant in an OOP way using `new` | ||
- `Err` class for you to construct an `Err` variant in an OOP way using `new` | ||
- `Result` type - only available in TypeScript | ||
- `Ok` class and type | ||
- `Err` class and type | ||
- `Result` Type as well as namespace / object from which to call [`Result.fromThrowable`](#resultfromthrowable-static-class-method) | ||
- `ResultAsync` class | ||
@@ -789,5 +789,5 @@ - `okAsync` convenience function to create a `ResultAsync` containing an `Ok` type `Result` | ||
`_unsafeUnwrap` takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws. | ||
`_unsafeUnwrap` takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object. | ||
`_unsafeUnwrapErr` takes a `Result<T, E>` and returns a `E` when the result is an `Err`, otherwise it throws. | ||
`_unsafeUnwrapErr` takes a `Result<T, E>` and returns a `E` when the result is an `Err`, otherwise it throws a custom object. | ||
@@ -802,3 +802,13 @@ That way you can do something like: | ||
By default, the thrown value does not contain a stack trace. This is because stack trace generation [makes error messages in Jest harder to understand](https://github.com/supermacro/neverthrow/pull/215). If you want stack traces to be generated, call `_unsafeUnwrap` and / or `_unsafeUnwrapErr` with a config object: | ||
```typescript | ||
_unsafeUnwrapErr({ | ||
withStackTrace: true, | ||
}) | ||
// ^ Now the error object will have a `.stack` property containing the current stack | ||
``` | ||
--- | ||
@@ -805,0 +815,0 @@ |
54655
711
826