Skip to content

Commit

Permalink
feat: use IllegalStateError instead of AssertionError
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMalch committed Oct 30, 2020
1 parent e0daabb commit cc16ac3
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ export function isDefined<T>(value: T): value is NonNullable<T> {
/* eslint-disable @typescript-eslint/no-explicit-any, new-cap */

/**
* Always throws an `AssertionError` with the given message.
* Always throws an `IllegalStateError` with the given message.
* It can come in handy when assigning values with a ternary operator or the null operators.
* @param message the message for the `AssertionError`
* @see AssertionError
* @param message the message for the `IllegalStateError`
* @see IllegalStateError
* @example
* function myFun(foo: string | null) {
* const bar = foo ?? error(PreconditionError, 'Argument may not be null');
Expand All @@ -215,26 +215,26 @@ export function error(
/**
* Always throws an error of the given type with the given message.
* It can come in handy when assigning values with a ternary operator or the null operators.
* @param errorType an error class, defaults to `AssertionError`
* @param errorType an error class
* @param message the error message
* @see AssertionError
* @see IllegalStateError
* @example
* function myFun(foo: string | null) {
* const bar = foo ?? error(PreconditionError, 'Argument may not be null');
* const result = bar.length > 0 ? 'OK' : error('Something went wrong!');
* }
*/
export function error(
errorType?: new (...args: any[]) => Error,
errorType: new (...args: any[]) => Error,
message?: string
): never;

export function error(
errorType: string | (new (...args: any[]) => Error) = AssertionError,
errorType?: string | (new (...args: any[]) => Error),
message?: string
): never {
throw typeof errorType === 'string'
? new AssertionError(errorType)
throw errorType == null || typeof errorType === 'string'
? new IllegalStateError(errorType)
: new errorType(message);
}

Expand Down

0 comments on commit cc16ac3

Please sign in to comment.