Skip to content

Commit

Permalink
fix: ClassProvider resolve parameter exception message lost
Browse files Browse the repository at this point in the history
  • Loading branch information
AEPKILL committed Jun 28, 2024
1 parent 2657c46 commit b9cbe8d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
33 changes: 32 additions & 1 deletion src/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
LifecycleEnum,
createContainer,
createServiceIdentifier,
injectable
inject,
injectable,
formatStringsWithIndent
} from "..";

describe("test middleware", () => {
Expand Down Expand Up @@ -41,4 +43,33 @@ describe("test middleware", () => {
expect(() => container.resolve(test1)).not.toThrow();
expect(container.resolve(test1)).toBe("Test1");
});

test("middleware error", () => {
const container = createContainer("test");

const test2 = createServiceIdentifier<string>(Symbol("test2"));

@injectable()
class Test1 {
constructor(@inject(test2) readonly test2: string) {}
}

container.addMiddleware((next) => (params) => {
const { serviceIdentifier } = params;

if (serviceIdentifier === test2) throw new Error("test2");

return next(params);
});

expect(() => container.resolve(Test1)).toThrow(
"Test1[#test]\n" +
formatStringsWithIndent([
"resolve service identifier Test1[#test]",
`service identifier \"Test1\" is not registered, but it is a constructor, use temporary class provider to resolve`,
`resolve parameter #0 of constructor Test1`,
`test2`
])
);
});
});
16 changes: 8 additions & 8 deletions src/providers/class.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export class ClassProvider<T> extends ProviderBase<T> {

resolve({ container, resolveRecordManager }: ProviderResolveOptions): T {
const parameter = this._parametersMetadata.map((it, index) => {
try {
resolveRecordManager.pushResolveRecord({
message: `resolve parameter #${index} of constructor ${this._classConstructor.name}`
});
resolveRecordManager.pushResolveRecord({
message: `resolve parameter #${index} of constructor ${this._classConstructor.name}`
});

return container.resolve(it.serviceIdentifier, it);
} finally {
resolveRecordManager.popResolveRecord();
}
const value = container.resolve(it.serviceIdentifier, it);

resolveRecordManager.popResolveRecord();

return value;
});

try {
Expand Down

0 comments on commit b9cbe8d

Please sign in to comment.