Skip to content
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
7 changes: 5 additions & 2 deletions src/lualib/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ interface ErrorType {
new (...args: any[]): Error;
}

function getErrorStack(constructor: () => any): string {
function getErrorStack(constructor: () => any): string | undefined {
// If debug module is not available in this environment, don't bother trying to get stack trace
if (debug === undefined) return undefined;

let level = 1;
while (true) {
const info = debug.getinfo(level, "f");
Expand Down Expand Up @@ -49,7 +52,7 @@ function initErrorClass(Type: ErrorType, name: string): any {
export const Error: ErrorConstructor = initErrorClass(
class implements Error {
public name = "Error";
public stack: string;
public stack?: string;

constructor(public message = "") {
this.stack = getErrorStack((this.constructor as any).new);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,22 @@ test.each([...builtinErrors, "CustomError"])("get stack from %s", errorType => {
expect(stack).toMatch("innerFunction");
expect(stack).toMatch("outerFunction");
});

test("still works without debug module", () => {
util.testFunction`
try
{
throw new Error("hello, world");
}
catch (e)
{
return e;
}
`
.setLuaHeader("debug = nil")
.expectToEqual({
message: "hello, world",
name: "Error",
stack: undefined,
});
});