forked from microsoft/tsyringe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error messages information (microsoft#70) (microsoft#88)
* Improve error messages information (microsoft#70) * Improve multiline regexp readability * Fix params info type definition
- Loading branch information
1 parent
4f63bb3
commit 75f769d
Showing
6 changed files
with
119 additions
and
26 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
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,60 @@ | ||
import {instance as globalContainer} from "../dependency-container"; | ||
import {inject, injectable} from "../decorators"; | ||
|
||
afterEach(() => { | ||
globalContainer.reset(); | ||
}); | ||
|
||
test("Error message composition", () => { | ||
class Ok {} | ||
|
||
@injectable() | ||
class C { | ||
constructor(public s: any) {} | ||
} | ||
|
||
@injectable() | ||
class B { | ||
constructor(public c: C) {} | ||
} | ||
|
||
@injectable() | ||
class A { | ||
constructor(public d: Ok, public b: B) {} | ||
} | ||
|
||
expect(() => { | ||
globalContainer.resolve(A); | ||
}).toThrow( | ||
new RegExp( | ||
[ | ||
/Cannot inject the dependency "b" at position #1 of "A" constructor\. Reason:/, | ||
/Cannot inject the dependency "c" at position #0 of "B" constructor\. Reason:/, | ||
/Cannot inject the dependency "s" at position #0 of "C" constructor\. Reason:/, | ||
/TypeInfo not known for "Object"/ | ||
] | ||
.map(x => x.source) | ||
.join("\\s+") | ||
) | ||
); | ||
}); | ||
|
||
test("Param position", () => { | ||
@injectable() | ||
class A { | ||
constructor(@inject("missing") public j: any) {} | ||
} | ||
|
||
expect(() => { | ||
globalContainer.resolve(A); | ||
}).toThrow( | ||
new RegExp( | ||
[ | ||
/Cannot inject the dependency "j" at position #0 of "A" constructor\. Reason:/, | ||
/Attempted to resolve unregistered dependency token: "missing"/ | ||
] | ||
.map(x => x.source) | ||
.join("\\s+") | ||
) | ||
); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import constructor from "./types/constructor"; | ||
|
||
function formatDependency(params: string | null, idx: number): string { | ||
if (params === null) { | ||
return `at position #${idx}`; | ||
} | ||
const argName = params.split(",")[idx].trim(); | ||
return `"${argName}" at position #${idx}`; | ||
} | ||
|
||
function composeErrorMessage(msg: string, e: Error, indent = " "): string { | ||
return [msg, ...e.message.split("\n").map(l => indent + l)].join("\n"); | ||
} | ||
|
||
export function formatErrorCtor( | ||
ctor: constructor<any>, | ||
paramIdx: number, | ||
error: Error | ||
): string { | ||
const [, params = null] = | ||
ctor.toString().match(/constructor\(([\w, ]+)\)/) || []; | ||
const dep = formatDependency(params, paramIdx); | ||
return composeErrorMessage( | ||
`Cannot inject the dependency ${dep} of "${ctor.name}" constructor. Reason:`, | ||
error | ||
); | ||
} |
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