All files / src/error index.js

100% Statements 118/118
100% Branches 19/19
100% Functions 9/9
100% Lines 118/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 1341x   1x 1x   1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 200x   200x 200x 89x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x   1x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x 1x 1x 1x 1x 1x 1x 1x 20x 20x   1x  
// @ts-check
 
import { getTypeSignature, getDefinedConstructorName } from '../utility';
import { hasMatchingErrorPrototype } from './utility';
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
 
/** @typedef {import('./typedef.js').AnyError} AnyError */
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AnyError}
 *  whether the passed value matches any error type, hence it is
 *  an instance, either of the basic `Error` type, or of one of the
 *  built-in error-type subclasses (`SyntaxError`, `ReferenceError`, etc.),
 *  or of a custom error-type that extends the basic `Error` type.
 * @category Error Type Detection
 */
export function isError(value) {
  const signature = getTypeSignature(value);
 
  return (
    signature === '[object Error]' ||
    (signature === '[object Object]' && hasMatchingErrorPrototype(value))
  );
  // return getTypeSignature(value) === '[object Error]';
  // //
  // // - the above commented approach does not cover
  // //   the error test-suite edge cases of ...
  // //    - `Object.create(Error.prototype)`
  // //    - and an ES3 style `LegacyError` like ...
  // //      ```
  // //      (() => {
  // //        function LegacyError() {}
  // //        LegacyError.prototype = Object.create(Error.prototype);
  // //        LegacyError.prototype.name = 'LegacyError';
  // //        return new LegacyError();
  // //      })(),
  // //      ```
}
 
/** @typedef {import('./typedef.js').PlainError} PlainError */
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is PlainError}
 *  whether the passed value matches exactly the built-in basic `Error` type.
 * @category Error Type Detection
 */
export function isErrorError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'Error';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is EvalError}
 *  whether the passed value matches exactly the built-in `EvalError` subtype.
 * @category Error Type Detection
 */
export function isEvalError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'EvalError';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is RangeError}
 *  whether the passed value matches exactly the built-in `RangeError` subtype.
 * @category Error Type Detection
 */
export function isRangeError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'RangeError';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is ReferenceError}
 *  whether the passed value matches exactly the built-in `ReferenceError` subtype.
 * @category Error Type Detection
 */
export function isReferenceError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'ReferenceError';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is SyntaxError}
 *  whether the passed value matches exactly the built-in `SyntaxError` subtype.
 * @category Error Type Detection
 */
export function isSyntaxError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'SyntaxError';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is TypeError}
 *  whether the passed value matches exactly the built-in `TypeError` subtype.
 * @category Error Type Detection
 */
export function isTypeError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'TypeError';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is URIError}
 *  whether the passed value matches exactly the built-in `URIError` subtype.
 * @category Error Type Detection
 */
export function isURIError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'URIError';
}
 
/**
 * @param {any} [value]
 *  An optionally passed value of any type.
 * @returns {value is AggregateError}
 *  whether the passed value matches exactly the built-in `AggregateError` subtype.
 * @category Error Type Detection
 */
export function isAggregateError(value) {
  return isError(value) && getDefinedConstructorName(value) === 'AggregateError';
}
 
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----