@launchtray/tsyringe-async
Version:
Lightweight dependency injection container for JavaScript/TypeScript, with asynchronous resolution
29 lines (28 loc) • 1.06 kB
JavaScript
function formatDependency(params, idx) {
if (params == null) {
return `at position #${idx}`;
}
const argName = params.split(",")[idx].trim();
return `"${argName}" at position #${idx}`;
}
function composeErrorMessage(msg, e, indent = " ") {
return [msg, ...e.message.split("\n").map(l => indent + l)].join("\n");
}
export function formatErrorCtor(ctor, paramIdx, error, propertyKey = undefined) {
let params;
let targetName;
if (propertyKey) {
const methodName = String(propertyKey);
[, params] =
ctor.constructor
.toString()
.match(new RegExp(`${methodName}\\(([\\w, ]+)\\)`)) || [];
targetName = `${ctor.constructor.name}.${methodName}`;
}
else {
[, params] = ctor.toString().match(/constructor\(([\w, ]+)\)/) || [];
targetName = `${ctor.name} constructor`;
}
const dep = formatDependency(params, paramIdx);
return composeErrorMessage(`Cannot inject the dependency ${dep} of ${targetName}. Reason:`, error);
}