Skip to content

Commit

Permalink
error stack
Browse files Browse the repository at this point in the history
  • Loading branch information
matuszeman committed Mar 27, 2017
1 parent ca1e8e0 commit 1125f0f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
19 changes: 11 additions & 8 deletions es5/dic.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,11 @@ var Dic = function () {
}, {
key: 'throwError',
value: function throwError(msg, stack) {
var stackStr = stack.join(' > ');
throw new Error(this.getDicInstanceName() + ': ' + msg + ' [' + stackStr + ']');
var stackStr = '';
if (stack) {
stackStr = '[' + stack.join(' > ') + ']';
}
throw new Error(this.getDicInstanceName() + ': ' + msg + ' ' + stackStr);
}

/**
Expand Down Expand Up @@ -442,11 +445,11 @@ var Dic = function () {
}

if (def.type === 'asyncFactory' && !def.asyncInitialized) {
this.throwError('Async factory for ' + name + ' must be run first. Run dic.asyncInit()');
this.throwError('Async factory for ' + name + ' must be run first. Run dic.asyncInit()', opts.stack);
}

if (!opts.ignoreAsync && def.asyncInit && !def.asyncInitialized) {
this.throwError('Instance "' + name + '" is not async initialized yet');
this.throwError('Instance "' + name + '" is not async initialized yet', opts.stack);
}

if (def.instance) {
Expand All @@ -458,7 +461,7 @@ var Dic = function () {
//test for possible init method but without init enabled
if (this.hasAsyncInit(instance)) {
if (_.isUndefined(def.asyncInit)) {
this.throwError(name + ' has got ' + name + '.asyncInit() method. Did you forget to mark this instance to be async initialized?');
this.throwError(name + ' has got ' + name + '.asyncInit() method. Did you forget to mark this instance to be async initialized?', opts.stack);
} else if (!def.asyncInit) {
console.warn(name + ' has got ' + name + '.asyncInit() method but auto init is disabled. Make sure you init the service manually yourself.');
}
Expand Down Expand Up @@ -701,7 +704,7 @@ var Dic = function () {

switch (def.type) {
case 'asyncFactory':
this.throwError('Use dic.createInstanceAsync() instead');
this.throwError('Use dic.createInstanceAsync() instead', opts.stack);
break;
case 'factory':
return (_def = def).factory.apply(_def, _toConsumableArray(this.getServices(def.params, def.inject, opts)));
Expand All @@ -710,7 +713,7 @@ var Dic = function () {
return new (Function.prototype.bind.apply(def.class, [null].concat(_toConsumableArray(this.getServices(def.params, def.inject, opts)))))();
break;
default:
this.throwError('Unknown instance def type: ' + def.type);
this.throwError('Unknown instance def type: ' + def.type, opts.stack);
}
}

Expand Down Expand Up @@ -782,7 +785,7 @@ var Dic = function () {
return _context3.abrupt('return', new _context3.t18());

case 36:
this.throwError('Unknown instance def type: ' + def.type);
this.throwError('Unknown instance def type: ' + def.type, opts.stack);

case 37:
case 'end':
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bb-dic",
"version": "0.5.0",
"version": "0.5.1",
"description": "A dependency injection container",
"main": "src/index.js",
"scripts": {
Expand Down
19 changes: 11 additions & 8 deletions src/dic.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,11 @@ class Dic {
}

throwError(msg, stack) {
const stackStr = stack.join(' > ');
throw new Error(`${this.getDicInstanceName()}: ${msg} [${stackStr}]`);
let stackStr = '';
if (stack) {
stackStr = '[' + stack.join(' > ') + ']';
}
throw new Error(`${this.getDicInstanceName()}: ${msg} ${stackStr}`);
}

/**
Expand All @@ -344,11 +347,11 @@ class Dic {
}

if (def.type === 'asyncFactory' && !def.asyncInitialized) {
this.throwError(`Async factory for ${name} must be run first. Run dic.asyncInit()`);
this.throwError(`Async factory for ${name} must be run first. Run dic.asyncInit()`, opts.stack);
}

if (!opts.ignoreAsync && def.asyncInit && !def.asyncInitialized) {
this.throwError(`Instance "${name}" is not async initialized yet`);
this.throwError(`Instance "${name}" is not async initialized yet`, opts.stack);
}

if (def.instance) {
Expand All @@ -360,7 +363,7 @@ class Dic {
//test for possible init method but without init enabled
if (this.hasAsyncInit(instance)) {
if (_.isUndefined(def.asyncInit)) {
this.throwError(`${name} has got ${name}.asyncInit() method. Did you forget to mark this instance to be async initialized?`);
this.throwError(`${name} has got ${name}.asyncInit() method. Did you forget to mark this instance to be async initialized?`, opts.stack);
} else if (!def.asyncInit) {
console.warn(`${name} has got ${name}.asyncInit() method but auto init is disabled. Make sure you init the service manually yourself.`);
}
Expand Down Expand Up @@ -537,7 +540,7 @@ class Dic {

switch(def.type) {
case 'asyncFactory':
this.throwError('Use dic.createInstanceAsync() instead');
this.throwError('Use dic.createInstanceAsync() instead', opts.stack);
break;
case 'factory':
return def.factory(...(this.getServices(def.params, def.inject, opts)));
Expand All @@ -546,7 +549,7 @@ class Dic {
return new (def.class)(...(this.getServices(def.params, def.inject, opts)));
break;
default:
this.throwError(`Unknown instance def type: ${def.type}`);
this.throwError(`Unknown instance def type: ${def.type}`, opts.stack);
}
}

Expand All @@ -571,7 +574,7 @@ class Dic {
return new (def.class)(...(await this.getServicesAsync(def.params, def.inject, opts)));
break;
default:
this.throwError(`Unknown instance def type: ${def.type}`);
this.throwError(`Unknown instance def type: ${def.type}`, opts.stack);
}
}

Expand Down

0 comments on commit 1125f0f

Please sign in to comment.