Skip to content

Commit bc4ce20

Browse files
committed
feat(context): reports the resolution path for circular deps
1 parent 8fae758 commit bc4ce20

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

packages/context/src/resolution-session.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ export class ResolutionSession {
228228
debugSession('Enter binding:', binding.toJSON());
229229
}
230230
if (this.stack.find(i => i.type === 'binding' && i.value === binding)) {
231-
throw new Error(
232-
`Circular dependency detected on path '${this.getBindingPath()} --> ${
233-
binding.key
234-
}'`,
235-
);
231+
const msg =
232+
`Circular dependency detected: ` +
233+
`${this.getResolutionPath()} --> ${binding.key}`;
234+
debugSession(msg);
235+
throw new Error(msg);
236236
}
237237
this.stack.push({type: 'binding', value: binding});
238238
/* istanbul ignore if */

packages/context/test/unit/resolver.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ describe('constructor injection', () => {
107107
context.bind('x').toClass(XClass);
108108
context.bind('y').toClass(YClass);
109109
expect(() => context.getSync('x')).to.throw(
110-
"Circular dependency detected on path 'x --> y --> x'",
110+
'Circular dependency detected: x --> @XClass.prototype.y ' +
111+
'--> y --> @YClass.prototype.x --> x',
111112
);
112113
expect(() => context.getSync('y')).to.throw(
113-
"Circular dependency detected on path 'y --> x --> y'",
114+
'Circular dependency detected: y --> @YClass.prototype.x ' +
115+
'--> x --> @XClass.prototype.y --> y',
114116
);
115117
});
116118

@@ -138,13 +140,16 @@ describe('constructor injection', () => {
138140
context.bind('y').toClass(YClass);
139141
context.bind('z').toClass(ZClass);
140142
expect(() => context.getSync('x')).to.throw(
141-
"Circular dependency detected on path 'x --> y --> z --> x'",
143+
'Circular dependency detected: x --> @XClass.constructor[0] --> y ' +
144+
'--> @YClass.constructor[0] --> z --> @ZClass.constructor[0] --> x',
142145
);
143146
expect(() => context.getSync('y')).to.throw(
144-
"Circular dependency detected on path 'y --> z --> x --> y'",
147+
'Circular dependency detected: y --> @YClass.constructor[0] --> z ' +
148+
'--> @ZClass.constructor[0] --> x --> @XClass.constructor[0] --> y',
145149
);
146150
expect(() => context.getSync('z')).to.throw(
147-
"Circular dependency detected on path 'z --> x --> y --> z'",
151+
'Circular dependency detected: z --> @ZClass.constructor[0] --> x ' +
152+
'--> @XClass.constructor[0] --> y --> @YClass.constructor[0] --> z',
148153
);
149154
});
150155

0 commit comments

Comments
 (0)