UNPKG

graphql-modules

Version:

Create reusable, maintainable, testable and extendable GraphQL modules

88 lines (87 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.invalidProviderError = invalidProviderError; exports.noInjectableError = noInjectableError; exports.noAnnotationError = noAnnotationError; exports.cyclicDependencyError = cyclicDependencyError; exports.noProviderError = noProviderError; exports.instantiationError = instantiationError; const utils_1 = require("./utils"); function invalidProviderError(provider) { return Error(`Invalid provider - only instances of Provider and Type are allowed, got: ${provider}`); } function noInjectableError(type) { return Error(`Missing @Injectable decorator for '${(0, utils_1.stringify)(type)}'`); } function noAnnotationError(typeOrFunc, params) { const signature = []; for (let i = 0, len = params.length; i < len; i++) { const parameter = params[i]; if (!parameter.type) { signature.push('?'); } else { signature.push((0, utils_1.stringify)(parameter.type)); } } return Error("Cannot resolve all parameters for '" + (0, utils_1.stringify)(typeOrFunc) + "'(" + signature.join(', ') + '). ' + "Make sure that all the parameters are decorated with Inject or have valid type annotations and that '" + (0, utils_1.stringify)(typeOrFunc) + "' is decorated with Injectable."); } function cyclicDependencyError(injector, key) { return injectionError(injector, key, function () { return `Cannot instantiate cyclic dependency!${constructResolvingPath(this.keys)}`; }); } function noProviderError(injector, key) { return injectionError(injector, key, function () { const first = (0, utils_1.stringify)(this.keys[0].token); return `No provider for ${first}!${constructResolvingPath(this.keys)}`; }); } function instantiationError(injector, originalException, key) { return injectionError(injector, key, function () { const first = (0, utils_1.stringify)(this.keys[0].token); return `Error during instantiation of ${first}: ${(0, utils_1.getOriginalError)(this).message}${constructResolvingPath(this.keys)}`; }, originalException); } function injectionError(injector, key, constructResolvingMessage, originalError) { const error = (originalError ? (0, utils_1.wrappedError)('', originalError) : Error()); error.addKey = addKey; error.keys = [key]; error.constructResolvingMessage = function wrappedConstructResolvingMessage() { return (constructResolvingMessage.call(this) + ` - in ${injector.displayName}`); }; error.message = error.constructResolvingMessage(); error[utils_1.ERROR_ORIGINAL_ERROR] = originalError; return error; } function constructResolvingPath(keys) { if (keys.length > 1) { const reversed = findFirstClosedCycle(keys.slice().reverse()); const tokenStrs = reversed.map((k) => (0, utils_1.stringify)(k.token)); return ' (' + tokenStrs.join(' -> ') + ')'; } return ''; } function findFirstClosedCycle(keys) { const res = []; for (let i = 0; i < keys.length; ++i) { if (res.indexOf(keys[i]) > -1) { res.push(keys[i]); return res; } res.push(keys[i]); } return res; } function addKey(key) { this.keys.push(key); this.message = this.constructResolvingMessage(); }