Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update planner error description with better error messages #1625

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mocha.opts
prettier.config.mjs
PULL_REQUEST_TEMPLATE.md
renovate.json
scripts
src
temp
test
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- Updated planner with better error description when a binding can not be properly resolved.

### Fixed

Expand Down
5 changes: 3 additions & 2 deletions src/constants/error_msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export const AMBIGUOUS_MATCH: string =
export const CANNOT_UNBIND: string = 'Could not unbind serviceIdentifier:';
export const NOT_REGISTERED: string =
'No matching bindings found for serviceIdentifier:';
export const MISSING_INJECTABLE_ANNOTATION: string =
'Missing required @injectable annotation in:';
export const TRYING_TO_RESOLVE_BINDINGS: (name: string) => string = (
name: string,
) => `Trying to resolve bindings for "${name}"`;
export const UNDEFINED_INJECT_ANNOTATION: (name: string) => string = (
name: string,
) =>
Expand Down
7 changes: 7 additions & 0 deletions src/planning/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function _getActiveBindings(
_validateActiveBindingCount(
target.serviceIdentifier,
activeBindings,
parentRequest,
target,
context.container,
);
Expand Down Expand Up @@ -144,6 +145,7 @@ function _getTargetMetadata(
function _validateActiveBindingCount(
serviceIdentifier: interfaces.ServiceIdentifier,
bindings: interfaces.Binding<unknown>[],
parentRequest: interfaces.Request | null,
target: interfaces.Target,
container: interfaces.Container,
): interfaces.Binding<unknown>[] {
Expand All @@ -161,6 +163,11 @@ function _validateActiveBindingCount(
serviceIdentifierString,
getBindings,
);

if (parentRequest !== null) {
msg += `\n${ERROR_MSGS.TRYING_TO_RESOLVE_BINDINGS(getServiceIdentifierAsString(parentRequest.serviceIdentifier))}`;
}

throw new Error(msg);
}

Expand Down
3 changes: 2 additions & 1 deletion test/planning/planner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,8 @@ describe('Planner', () => {
};

expect(throwFunction).to.throw(
'No matching bindings found for serviceIdentifier: Function',
`No matching bindings found for serviceIdentifier: Function
Trying to resolve bindings for "Ninja"`,
);
});
});