cause.js
This file
Path
lib/return/cause.js
37 lines · 1.2 KiB · JavaScript
Last changed by
Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
History of this fileTake it
Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 1–37why, one step back1export const getFinalError = (originalError, message, isSync) => {2 const ErrorClass = isSync ? ExecaSyncError : ExecaError;3 const options = originalError instanceof DiscardedError ? {} : {cause: originalError};4 return new ErrorClass(message, options);5};67// Indicates that the error is used only to interrupt control flow, but not in the return value8export class DiscardedError extends Error {}910// Proper way to set `error.name`: it should be inherited and non-enumerable11const setErrorName = (ErrorClass, value) => {12 Object.defineProperty(ErrorClass.prototype, 'name', {13 value,14 writable: true,15 enumerable: false,16 configurable: true,17 });18 Object.defineProperty(ErrorClass.prototype, execaErrorSymbol, {19 value: true,20 writable: false,21 enumerable: false,22 configurable: false,23 });24};2526// Unlike `instanceof`, this works across realms27export const isExecaError = error => isErrorInstance(error) && execaErrorSymbol in error;2829const execaErrorSymbol = Symbol('isExecaError');3031export const isErrorInstance = value => Object.prototype.toString.call(value) === '[object Error]';3233export class ExecaError extends Error {}34setErrorName(ExecaError, ExecaError.name);3536export class ExecaSyncError extends Error {}37setErrorName(ExecaSyncError, ExecaSyncError.name);