Skip to content

Commit

Permalink
Merge pull request #24 from abraham/never-error
Browse files Browse the repository at this point in the history
Switch to NeverError subclass of Error
  • Loading branch information
abraham authored Aug 1, 2018
2 parents b391244 + c239cb6 commit 57b7592
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ test('fold unknown', () => {
successMock,
);

expect(() => view(otherMock)).toThrowError('Unknown RemoteData type used');
expect(() => view(otherMock)).toThrowError('Unknown RemoteData state:');
});
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Failure<E> {

constructor(public error: E) {
if (error === null || error === undefined) {
fail('Parameter "error" is required');
throw new Error('Parameter "error" is required');
}
}
}
Expand All @@ -30,7 +30,7 @@ export class Success<D> {

constructor(public data: D) {
if (data === null || data === undefined) {
fail('Parameter "data" is required');
throw new Error('Parameter "data" is required');
}
}
}
Expand All @@ -52,11 +52,13 @@ export function fold<T, E, D>(
case Kinds.Success:
return success(state.data);
default:
return fail('Unknown RemoteData type used');
throw new NeverError(state);
}
}
}

function fail(error: string): never {
throw new Error(error);
class NeverError extends Error {
constructor(value: never) {
super(`Unknown RemoteData state: ${value}`);
}
}

0 comments on commit 57b7592

Please sign in to comment.