UNPKG

awilix

Version:

Extremely powerful dependency injection container.

151 lines 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AwilixRegistrationError = exports.AwilixResolutionError = exports.AwilixTypeError = exports.AwilixError = exports.ExtendableError = void 0; /** * Newline. */ const EOL = '\n'; /** * An extendable error class. * @author https://github.com/bjyoungblood/es6-error/ */ class ExtendableError extends Error { /** * Constructor for the error. * * @param {String} message * The error message. */ constructor(message) { super(message); // extending Error is weird and does not propagate `message` Object.defineProperty(this, 'message', { enumerable: false, value: message, }); Object.defineProperty(this, 'name', { enumerable: false, value: this.constructor.name, }); // Not all browsers have this function. /* istanbul ignore else */ if ('captureStackTrace' in Error) { Error.captureStackTrace(this, this.constructor); } else { Object.defineProperty(this, 'stack', { enumerable: false, value: Error(message).stack, writable: true, configurable: true, }); } } } exports.ExtendableError = ExtendableError; /** * Base error for all Awilix-specific errors. */ class AwilixError extends ExtendableError { } exports.AwilixError = AwilixError; /** * Error thrown to indicate a type mismatch. */ class AwilixTypeError extends AwilixError { /** * Constructor, takes the function name, expected and given * type to produce an error. * * @param {string} funcDescription * Name of the function being guarded. * * @param {string} paramName * The parameter there was an issue with. * * @param {string} expectedType * Name of the expected type. * * @param {string} givenType * Name of the given type. */ constructor(funcDescription, paramName, expectedType, givenType) { super(`${funcDescription}: expected ${paramName} to be ${expectedType}, but got ${givenType}.`); } /** * Asserts the given condition, throws an error otherwise. * * @param {*} condition * The condition to check * * @param {string} funcDescription * Name of the function being guarded. * * @param {string} paramName * The parameter there was an issue with. * * @param {string} expectedType * Name of the expected type. * * @param {string} givenType * Name of the given type. */ static assert(condition, funcDescription, paramName, expectedType, givenType) { if (!condition) { throw new AwilixTypeError(funcDescription, paramName, expectedType, givenType); } return condition; } } exports.AwilixTypeError = AwilixTypeError; /** * A nice error class so we can do an instanceOf check. */ class AwilixResolutionError extends AwilixError { /** * Constructor, takes the registered modules and unresolved tokens * to create a message. * * @param {string|symbol} name * The name of the module that could not be resolved. * * @param {string[]} resolutionStack * The current resolution stack */ constructor(name, resolutionStack, message) { const stringName = name.toString(); const nameStack = resolutionStack.map(({ name: val }) => val.toString()); nameStack.push(stringName); const resolutionPathString = nameStack.join(' -> '); let msg = `Could not resolve '${stringName}'.`; if (message) { msg += ` ${message}`; } msg += EOL + EOL; msg += `Resolution path: ${resolutionPathString}`; super(msg); } } exports.AwilixResolutionError = AwilixResolutionError; /** * A nice error class so we can do an instanceOf check. */ class AwilixRegistrationError extends AwilixError { /** * Constructor, takes the registered modules and unresolved tokens * to create a message. * * @param {string|symbol} name * The name of the module that could not be registered. */ constructor(name, message) { const stringName = name.toString(); let msg = `Could not register '${stringName}'.`; if (message) { msg += ` ${message}`; } super(msg); } } exports.AwilixRegistrationError = AwilixRegistrationError; //# sourceMappingURL=errors.js.map