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)
- Loading branch information
1 parent
4f63bb3
commit 6fbd5e6
Showing
5 changed files
with
92 additions
and
23 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,42 @@ | ||
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( | ||
/Cannot inject the dependency "b" at position #1 of "A" constructor. Reason:\s+Cannot inject the dependency "c" at position #0 of "B" constructor. Reason:\s+Cannot inject the dependency "s" at position #0 of "C" constructor. Reason:\s+TypeInfo not known for Object/ | ||
); | ||
}); | ||
|
||
test("Param position", () => { | ||
@injectable() | ||
class A { | ||
constructor(@inject("missing") public j: any) {} | ||
} | ||
expect(() => { | ||
globalContainer.resolve(A); | ||
}).toThrow( | ||
/Cannot inject the dependency "j" at position #0 of "A" constructor. Reason:\s+Attempted to resolve unregistered dependency token: "missing"/ | ||
); | ||
}); |
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 | ||
); | ||
} |