Skip to content

Commit

Permalink
feat(exhaustive): rename thrown error to NonExhaustiveError
Browse files Browse the repository at this point in the history
  • Loading branch information
gvergnaud committed Aug 11, 2024
1 parent b2e7566 commit 99aa90e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Error when the given input value does not match any included pattern
* and .exhaustive() was specified
*/
export class ExhaustiveError extends Error {
export class NonExhaustiveError extends Error {
constructor(public input: unknown) {
let displayedValue;
try {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import * as Pattern from './patterns';
export { match } from './match';
export { isMatching } from './is-matching';
export { Pattern, Pattern as P };
export { ExhaustiveError } from './errors';
export { NonExhaustiveError } from './errors';
4 changes: 2 additions & 2 deletions src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Pattern } from './types/Pattern';
import { Match } from './types/Match';
import * as symbols from './internals/symbols';
import { matchPattern } from './internals/helpers';
import { ExhaustiveError } from './errors';
import { NonExhaustiveError } from './errors';

type MatchState<output> =
| { matched: true; value: output }
Expand Down Expand Up @@ -115,7 +115,7 @@ class MatchExpression<input, output> {
exhaustive(): output {
if (this.state.matched) return this.state.value;

throw new ExhaustiveError(this.input);
throw new NonExhaustiveError(this.input);
}

run(): output {
Expand Down
12 changes: 6 additions & 6 deletions tests/exhaustive-match.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExhaustiveError, match, P } from '../src';
import { NonExhaustiveError, match, P } from '../src';
import { Equal, Expect } from '../src/types/helpers';
import {
Option,
Expand Down Expand Up @@ -808,31 +808,31 @@ describe('exhaustive()', () => {
.exhaustive();
}

it('Unmatched pattern results in ExhaustiveError', () => {
it('Unmatched pattern results in NonExhaustiveError', () => {
expect.assertions(3);
const input = { color: 'orange' } as unknown as FlagComponent;

try {
checkFlagComponent(input);
} catch (e) {
const err = e as ExhaustiveError;
expect(err).toBeInstanceOf(ExhaustiveError);
const err = e as NonExhaustiveError;
expect(err).toBeInstanceOf(NonExhaustiveError);
expect(err.message).toEqual(
'Pattern matching error: no pattern matches value {"color":"orange"}'
);
expect(err.input).toStrictEqual(input);
}
});

it('Matched pattern with callback error does not result in ExhaustiveError', () => {
it('Matched pattern with callback error does not result in NonExhaustiveError', () => {
expect.assertions(3);

try {
checkFlagComponent({ color: 'blue' });
} catch (e) {
const err = e as Error;
expect(err).toBeInstanceOf(Error);
expect(err).not.toBeInstanceOf(ExhaustiveError);
expect(err).not.toBeInstanceOf(NonExhaustiveError);
expect(err.message).toEqual('Blue error');
}
});
Expand Down

0 comments on commit 99aa90e

Please sign in to comment.