Skip to content

Commit

Permalink
chore: beautify error message
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Jun 4, 2024
1 parent 64e2184 commit 5c26b24
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/wzhudev/squirrel/master/src/schema/package.schema.json",
"name": "@wendellhu/redi",
"version": "0.15.2",
"version": "0.15.3",
"description": "A dependency library for TypeScript and JavaScript, along with a binding for React.",
"scripts": {
"test": "vitest run",
Expand Down
6 changes: 3 additions & 3 deletions src/dependencyCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ export class DependencyNotFoundForModuleError extends RediError {
const msg = `Cannot find "${prettyPrintIdentifier(id)}" registered by any injector. It is the ${index}th param of "${isIdentifierDecorator(toInstantiate)
? prettyPrintIdentifier(toInstantiate)
: (toInstantiate as Ctor<any>).name
}". The stack of dependencies is: "${ResolvingStack.map((id) => prettyPrintIdentifier(id)).join(' -> ')}".`
}".`

super(msg)

clearResolvingStack();
}
}

Expand All @@ -63,6 +61,8 @@ export class DependencyNotFoundError extends RediError {
const msg = `Cannot find "${prettyPrintIdentifier(id)}" registered by any injector. The stack of dependencies is: "${ResolvingStack.map((id) => prettyPrintIdentifier(id)).join(' -> ')}".`

super(msg)

clearResolvingStack();
}
}

Expand Down
18 changes: 5 additions & 13 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,12 @@ export class Injector {
): T[] | T | null {
this._ensureInjectorNotDisposed();

try {
const newResult = this._get(id, quantityOrLookup, lookUp)
if ((Array.isArray(newResult) && newResult.some((r) => isAsyncHook(r))) || isAsyncHook(newResult)) {
throw new GetAsyncItemFromSyncApiError(id)
}

return newResult as T | T[] | null
} catch (e: unknown) {
if (e instanceof DependencyNotFoundError) {
clearResolvingStack();
}

throw e;
const newResult = this._get(id, quantityOrLookup, lookUp)
if ((Array.isArray(newResult) && newResult.some((r) => isAsyncHook(r))) || isAsyncHook(newResult)) {
throw new GetAsyncItemFromSyncApiError(id)
}

return newResult as T | T[] | null
}

private _get<T>(
Expand Down
8 changes: 4 additions & 4 deletions test/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('core', () => {
class D { constructor(@Inject(C) private c: C) { } }

const j = new Injector([[B], [C], [D]]);
expectToThrow(() => j.get(D), 'Cannot find "A" registered by any injector. It is the 0th param of "B". The stack of dependencies is: "D -> C -> B -> A"');
expectToThrow(() => j.get(D), 'Cannot find "A" registered by any injector. It is the 0th param of "B".');
});

describe('basics', () => {
Expand Down Expand Up @@ -434,7 +434,7 @@ describe('core', () => {
}

const j = new Injector([[B]])
expect(() => { j.get(B) }).toThrow('[redi]: Cannot find "A" registered by any injector. It is the 1th param of "B". The stack of dependencies is: "B -> A".');
expect(() => { j.get(B) }).toThrow('[redi]: Cannot find "A" registered by any injector. It is the 1th param of "B".');
})
})

Expand Down Expand Up @@ -492,7 +492,7 @@ describe('core', () => {
])
expectToThrow(() => {
j.get(b)
}, '[redi]: Cannot find "A" registered by any injector. It is the 0th param of "b". The stack of dependencies is: "b -> A".')
}, '[redi]: Cannot find "A" registered by any injector. It is the 0th param of "b".')
})
})

Expand Down Expand Up @@ -963,7 +963,7 @@ describe('core', () => {

expectToThrow(
() => child.get(bI),
'[redi]: Cannot find "cI" registered by any injector. It is the 1th param of "bI". The stack of dependencies is: "bI".'
'[redi]: Cannot find "cI" registered by any injector. It is the 1th param of "bI".'
)
})

Expand Down

0 comments on commit 5c26b24

Please sign in to comment.