Skip to content

Commit

Permalink
Update planner error description with better error messages (#1625)
Browse files Browse the repository at this point in the history
* chore: ignore scripts folder

* feat: update planner with better error description

* docs: update changelog
  • Loading branch information
notaphplover authored Nov 12, 2024
1 parent c50565e commit 22cdcc0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
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"`,
);
});
});

0 comments on commit 22cdcc0

Please sign in to comment.