Skip to content

Commit 7641162

Browse files
author
Leon Si
committed
refactor!: rename _unsafeUnwrap to unwrapOrThrow
1 parent 068cee6 commit 7641162

File tree

6 files changed

+79
-79
lines changed

6 files changed

+79
-79
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ With `eslint-plugin-errok`, you are forced to consume the result in one of the f
138138

139139
- Calling `.match`
140140
- Calling `.unwrapOr`
141-
- Calling `._unsafeUnwrap`
141+
- Calling `.unwrapOrThrow`
142142

143143
This ensures that you're explicitly handling the error of your `Result`.
144144

@@ -1325,16 +1325,16 @@ For more information, see https://github.com/Tunnel-Labs/errok/pull/448 and http
13251325
13261326
### Testing
13271327
1328-
`Result` instances have two unsafe methods, aptly called `_unsafeUnwrap` and `_unsafeUnwrapErr` which **should only be used in a test environment**.
1328+
`Result` instances have two unsafe methods, aptly called `unwrapOrThrow` and `unwrapOrThrowErr` which **should only be used in a test environment**.
13291329
1330-
`_unsafeUnwrap` takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
1330+
`unwrapOrThrow` takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
13311331
1332-
`_unsafeUnwrapErr` takes a `Result<T, E>` and returns a `E` when the result is an `Err`, otherwise it throws a custom object.
1332+
`unwrapOrThrowErr` takes a `Result<T, E>` and returns a `E` when the result is an `Err`, otherwise it throws a custom object.
13331333
13341334
That way you can do something like:
13351335
13361336
```typescript
1337-
expect(myResult._unsafeUnwrap()).toBe(someExpectation);
1337+
expect(myResult.unwrapOrThrow()).toBe(someExpectation);
13381338
```
13391339
13401340
However, do note that `Result` instances are comparable. So you don't necessarily need to unwrap them in order to assert expectations in your tests. So you could also do something like this:
@@ -1349,10 +1349,10 @@ expect(callSomeFunctionThatReturnsAResult('with', 'some', 'args')).toEqual(
13491349
);
13501350
```
13511351
1352-
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/Tunnel-Labs/errok/pull/215). If you want stack traces to be generated, call `_unsafeUnwrap` and / or `_unsafeUnwrapErr` with a config object:
1352+
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/Tunnel-Labs/errok/pull/215). If you want stack traces to be generated, call `unwrapOrThrow` and / or `unwrapOrThrowErr` with a config object:
13531353
13541354
```typescript
1355-
_unsafeUnwrapErr({
1355+
unwrapOrThrowErr({
13561356
withStackTrace: true,
13571357
});
13581358

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "errok",
33
"type": "module",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"description": "Type-safe error handling",
66
"exports": {
77
"require": {

src/result-async.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
187187
return yield* await this._promise.then((res) => res.safeUnwrap());
188188
}
189189

190-
async _unsafeUnwrap() {
191-
return this._promise.then((res) => res._unsafeUnwrap());
190+
async unwrapOrThrow() {
191+
return this._promise.then((res) => res.unwrapOrThrow());
192192
}
193193

194194
// Makes ResultAsync implement PromiseLike<Result>

src/result.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ interface IResult<T, E> {
274274
*
275275
* @param config
276276
*/
277-
_unsafeUnwrap(config?: ErrorConfig): T;
277+
unwrapOrThrow(config?: ErrorConfig): T;
278278

279279
/**
280280
* **This method is unsafe, and should only be used in a test environments**
@@ -284,7 +284,7 @@ interface IResult<T, E> {
284284
*
285285
* @param config
286286
*/
287-
_unsafeUnwrapErr(config?: ErrorConfig): E;
287+
unwrapOrThrowErr(config?: ErrorConfig): E;
288288
}
289289

290290
export class Ok<T, E> implements IResult<T, E> {
@@ -351,13 +351,13 @@ export class Ok<T, E> implements IResult<T, E> {
351351
})();
352352
}
353353

354-
_unsafeUnwrap(_?: ErrorConfig): T {
354+
unwrapOrThrow(_?: ErrorConfig): T {
355355
return this.value;
356356
}
357357

358-
_unsafeUnwrapErr(config?: ErrorConfig): E {
358+
unwrapOrThrowErr(config?: ErrorConfig): E {
359359
throw createNeverThrowError(
360-
'Called `_unsafeUnwrapErr` on an Ok',
360+
'Called `unwrapOrThrowErr` on an Ok',
361361
this,
362362
config,
363363
);
@@ -429,11 +429,11 @@ export class Err<T, E> implements IResult<T, E> {
429429
})();
430430
}
431431

432-
_unsafeUnwrap(_?: ErrorConfig): T {
432+
unwrapOrThrow(_?: ErrorConfig): T {
433433
throw this.error;
434434
}
435435

436-
_unsafeUnwrapErr(_?: ErrorConfig): E {
436+
unwrapOrThrowErr(_?: ErrorConfig): E {
437437
return this.error;
438438
}
439439
}

0 commit comments

Comments
 (0)