Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add unwrapErrorOrThrow. #350

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/Result.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { defekt } from './defekt';

class ResultDoesNotContainError extends defekt({ code: 'ResultDoesNotContainError' }) {}

interface ResultBase<TValue, TError extends Error> {
hasError: () => this is ResultError<TValue, TError>;
hasValue: () => this is ResultValue<TValue, TError>;

unwrapOrThrow: (errorTransformer?: (err: TError) => Error) => TValue;
unwrapOrElse: (handleError: (error: Error) => TValue) => TValue;
unwrapOrDefault: (defaultValue: TValue) => TValue;

unwrapErrorOrThrow: () => TError;
}

interface ResultError<TValue, TError extends Error> extends ResultBase<TValue, TError> {
Expand Down Expand Up @@ -32,6 +38,9 @@ const error = function <TValue, TError extends Error>(err: TError): ResultError<
unwrapOrDefault (defaultValue: TValue): TValue {
return defaultValue;
},
unwrapErrorOrThrow (): TError {
return err;
},
error: err
};
};
Expand Down Expand Up @@ -60,6 +69,9 @@ const value: {
unwrapOrDefault (): TValue | undefined {
return val;
},
unwrapErrorOrThrow (): TError {
throw new ResultDoesNotContainError();
},
value: val
};
};
Expand All @@ -73,6 +85,8 @@ export type {
};

export {
ResultDoesNotContainError,

value,
error
};
3 changes: 2 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defekt } from './defekt';
import { isCustomError } from './isCustomError';
import { isError } from './isError';
import { isResult } from './isResult';
import { error, Result, value } from './Result';
import { error, Result, ResultDoesNotContainError, value } from './Result';

export {
CustomError,
Expand All @@ -13,6 +13,7 @@ export {
isCustomError,
isError,
isResult,
ResultDoesNotContainError,
value
};

Expand Down
5 changes: 3 additions & 2 deletions lib/isResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Result } from './Result';

const isResult = function (value: any): value is Result<any, any> {
return typeof value === 'object' && value !== null &&
Object.keys(value).length === 6 &&
Object.keys(value).length === 7 &&
('error' in value || 'value' in value) &&
'hasError' in value && typeof value.hasError === 'function' &&
'hasValue' in value && typeof value.hasValue === 'function' &&
'unwrapOrThrow' in value && typeof value.unwrapOrThrow === 'function' &&
'unwrapOrElse' in value && typeof value.unwrapOrElse === 'function' &&
'unwrapOrDefault' in value && typeof value.unwrapOrDefault === 'function';
'unwrapOrDefault' in value && typeof value.unwrapOrDefault === 'function' &&
'unwrapErrorOrThrow' in value && typeof value.unwrapErrorOrThrow === 'function';
};

export {
Expand Down
26 changes: 25 additions & 1 deletion test/unit/ResultTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert } from 'assertthat';
import { defekt } from '../../lib';
import { error, Result, value } from '../../lib/Result';
import { isCustomError } from '../../lib/isCustomError';
import { error, Result, ResultDoesNotContainError, value } from '../../lib/Result';

interface Value {
foo: string;
Expand Down Expand Up @@ -212,6 +213,29 @@ suite('Result', (): void => {
});
});

suite('unwrapErrorOrThrow', (): void => {
test('unwraps the error with the correct error type if the result contains an error.', async (): Promise<void> => {
class CustomError extends defekt({ code: 'CustomError' }) {}

const err = new CustomError();
const result = error(err) as Result<Value, CustomError>;

const containedError: CustomError = result.unwrapErrorOrThrow();

assert.that(containedError).is.equalTo(err);
});

test('throws a ResultDoesNotContainError error if the result does not contain an error.', async (): Promise<void> => {
class CustomError extends defekt({ code: 'CustomError' }) {}

const result = value({ foo: 'foo' }) as Result<Value, CustomError>;

assert.that((): void => {
result.unwrapErrorOrThrow();
}).is.throwing((ex: Error): boolean => isCustomError(ex, ResultDoesNotContainError));
});
});

suite('makes sense', (): void => {
test('a function that returns a Result can reasonably be used and the types make sense.', async (): Promise<void> => {
class CustomError extends defekt({ code: 'CustomError' }) {}
Expand Down