-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from inversify/feat/update-get-class-metadata-…
…modules-with-better-error-descriptions Update get class metadata modules with better error descriptions
- Loading branch information
Showing
10 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inversifyjs/core": patch | ||
--- | ||
|
||
Updated get metadata flow to provide better error messages when missing metadata. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ner/libraries/core/src/metadata/calculations/assertConstructorMetadataArrayFilled.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { beforeAll, describe, expect, it } from '@jest/globals'; | ||
|
||
import { InversifyCoreError } from '../../error/models/InversifyCoreError'; | ||
import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind'; | ||
import { ClassElementMetadata } from '../models/ClassElementMetadata'; | ||
import { assertConstructorMetadataArrayFilled } from './assertConstructorMetadataArrayFilled'; | ||
|
||
describe(assertConstructorMetadataArrayFilled.name, () => { | ||
describe('having an array with no empty values', () => { | ||
class Foo {} | ||
|
||
let arrayFixture: [ClassElementMetadata]; | ||
|
||
beforeAll(() => { | ||
arrayFixture = [Symbol() as unknown as ClassElementMetadata]; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
try { | ||
assertConstructorMetadataArrayFilled(Foo, arrayFixture); | ||
} catch (error: unknown) { | ||
result = error; | ||
} | ||
}); | ||
|
||
it('should not throw an error', () => { | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having an array with empty values', () => { | ||
class Foo {} | ||
|
||
let arrayFixture: (ClassElementMetadata | undefined)[]; | ||
|
||
beforeAll(() => { | ||
arrayFixture = new Array<ClassElementMetadata | undefined>(3); | ||
|
||
arrayFixture[1] = Symbol() as unknown as ClassElementMetadata; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
try { | ||
assertConstructorMetadataArrayFilled(Foo, arrayFixture); | ||
} catch (error: unknown) { | ||
result = error; | ||
} | ||
}); | ||
|
||
it('should throw an error', () => { | ||
const expectedErrorProperties: Partial<InversifyCoreError> = { | ||
kind: InversifyCoreErrorKind.missingInjectionDecorator, | ||
message: `Found unexpected missing metadata on type "Foo" at constructor indexes "0", "2". | ||
Are you using @inject, @multiInject or @unmanaged decorators at those indexes? | ||
If you're using typescript and want to rely on auto injection, set "emitDecoratorMetadata" compiler option to true`, | ||
}; | ||
|
||
expect(result).toStrictEqual( | ||
expect.objectContaining(expectedErrorProperties), | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...ontainer/libraries/core/src/metadata/calculations/assertConstructorMetadataArrayFilled.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Newable } from '@inversifyjs/common'; | ||
|
||
import { InversifyCoreError } from '../../error/models/InversifyCoreError'; | ||
import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind'; | ||
import { ClassElementMetadata } from '../models/ClassElementMetadata'; | ||
|
||
export function assertConstructorMetadataArrayFilled( | ||
type: Newable, | ||
value: (ClassElementMetadata | undefined)[], | ||
): asserts value is ClassElementMetadata[] { | ||
const undefinedIndexes: number[] = []; | ||
|
||
// Using a for loop to ensure empty values are traversed as well | ||
for (let i: number = 0; i < value.length; ++i) { | ||
const element: ClassElementMetadata | undefined = value[i]; | ||
|
||
if (element === undefined) { | ||
undefinedIndexes.push(i); | ||
} | ||
} | ||
|
||
if (undefinedIndexes.length > 0) { | ||
throw new InversifyCoreError( | ||
InversifyCoreErrorKind.missingInjectionDecorator, | ||
`Found unexpected missing metadata on type "${type.name}" at constructor indexes "${undefinedIndexes.join('", "')}". | ||
Are you using @inject, @multiInject or @unmanaged decorators at those indexes? | ||
If you're using typescript and want to rely on auto injection, set "emitDecoratorMetadata" compiler option to true`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters