UNPKG

3.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AwilixResolutionError = exports.AwilixTypeError = exports.AwilixError = exports.ExtendableError = void 0;
4/**
5 * Newline.
6 */
7const EOL = '\n';
8/**
9 * An extendable error class.
10 * @author https://github.com/bjyoungblood/es6-error/
11 */
12class ExtendableError extends Error {
13 /**
14 * Constructor for the error.
15 *
16 * @param {String} message
17 * The error message.
18 */
19 constructor(message) {
20 super(message);
21 // extending Error is weird and does not propagate `message`
22 Object.defineProperty(this, 'message', {
23 enumerable: false,
24 value: message,
25 });
26 Object.defineProperty(this, 'name', {
27 enumerable: false,
28 value: this.constructor.name,
29 });
30 Error.captureStackTrace(this, this.constructor);
31 }
32}
33exports.ExtendableError = ExtendableError;
34/**
35 * Base error for all Awilix-specific errors.
36 */
37class AwilixError extends ExtendableError {
38}
39exports.AwilixError = AwilixError;
40/**
41 * Error thrown to indicate a type mismatch.
42 * TODO(v3): remove `AwilixNotAFunctionError` and use this.
43 */
44class AwilixTypeError extends AwilixError {
45 /**
46 * Constructor, takes the function name, expected and given
47 * type to produce an error.
48 *
49 * @param {string} funcDescription
50 * Name of the function being guarded.
51 *
52 * @param {string} paramName
53 * The parameter there was an issue with.
54 *
55 * @param {string} expectedType
56 * Name of the expected type.
57 *
58 * @param {string} givenType
59 * Name of the given type.
60 */
61 constructor(funcDescription, paramName, expectedType, givenType) {
62 super(`${funcDescription}: expected ${paramName} to be ${expectedType}, but got ${givenType}.`);
63 }
64 /**
65 * Asserts the given condition, throws an error otherwise.
66 *
67 * @param {*} condition
68 * The condition to check
69 *
70 * @param {string} funcDescription
71 * Name of the function being guarded.
72 *
73 * @param {string} paramName
74 * The parameter there was an issue with.
75 *
76 * @param {string} expectedType
77 * Name of the expected type.
78 *
79 * @param {string} givenType
80 * Name of the given type.
81 */
82 static assert(condition, funcDescription, paramName, expectedType, givenType) {
83 if (!condition) {
84 throw new AwilixTypeError(funcDescription, paramName, expectedType, givenType);
85 }
86 return condition;
87 }
88}
89exports.AwilixTypeError = AwilixTypeError;
90/**
91 * A nice error class so we can do an instanceOf check.
92 */
93class AwilixResolutionError extends AwilixError {
94 /**
95 * Constructor, takes the registered modules and unresolved tokens
96 * to create a message.
97 *
98 * @param {string|symbol} name
99 * The name of the module that could not be resolved.
100 *
101 * @param {string[]} resolutionStack
102 * The current resolution stack
103 */
104 constructor(name, resolutionStack, message) {
105 if (typeof name === 'symbol') {
106 name = name.toString();
107 }
108 resolutionStack = resolutionStack.map((val) => typeof val === 'symbol' ? val.toString() : val);
109 resolutionStack.push(name);
110 const resolutionPathString = resolutionStack.join(' -> ');
111 let msg = `Could not resolve '${name}'.`;
112 if (message) {
113 msg += ` ${message}`;
114 }
115 msg += EOL + EOL;
116 msg += `Resolution path: ${resolutionPathString}`;
117 super(msg);
118 }
119}
120exports.AwilixResolutionError = AwilixResolutionError;
121//# sourceMappingURL=errors.js.map
\No newline at end of file