Skip to content

Commit e5d6234

Browse files
Stefan Botezedhzsz
Stefan Botez
authored andcommitted
Fix errors issues
1 parent 2a62749 commit e5d6234

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/Errors/index.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,18 @@ export const toFormatted = (error: unknown): string => {
8181
* user-provided enumerable properties.
8282
*/
8383
function _withEnumerableProperties(error: any) {
84-
if (error instanceof ExtendedError) {
84+
if (error instanceof Error) {
85+
const extendedError: ExtendedError = <ExtendedError>(<any>error);
8586
const ret: any = Object.assign(
8687
{
87-
errorType: error.name,
88-
errorMessage: error.message,
89-
code: error.code,
88+
errorType: extendedError.name,
89+
errorMessage: extendedError.message,
90+
code: extendedError.code,
9091
},
91-
error
92+
extendedError
9293
);
93-
if (typeof error.stack == "string") {
94-
ret.stack = error.stack.split("\n");
94+
if (typeof extendedError.stack == "string") {
95+
ret.stack = extendedError.stack.split("\n");
9596
}
9697
return ret;
9798
} else {

src/utils/UserFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function _loadUserApp(
101101
return _tryRequire(appRoot, moduleRoot, module);
102102
} catch (e) {
103103
if (e instanceof SyntaxError) {
104-
throw new UserCodeSyntaxError(e.message);
104+
throw new UserCodeSyntaxError(<any>e);
105105
} else if (e.code !== undefined && e.code === "MODULE_NOT_FOUND") {
106106
throw new ImportModuleError(e);
107107
} else {

test/unit/Errors/Errors.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ class CircularError extends Error {
1616
}
1717
}
1818

19-
describe("Formatting Error Logging", () => {
19+
class ExtendedError extends Error {
20+
code?: number;
21+
customProperty?: string;
22+
23+
constructor(message?: string) {
24+
super(message);
25+
26+
this.name = "ExtendedError";
27+
this.stack = "ExtendedErrorStack";
28+
this.code = 100;
29+
this.customProperty = "ExtendedErrorCustomProperty";
30+
}
31+
}
32+
33+
describe("Formatting CircularError Logging", () => {
2034
it("should fall back to a minimal error format when an exception occurs", () => {
2135
const error = new CircularError("custom message");
2236
error.backlink = error;
@@ -29,6 +43,22 @@ describe("Formatting Error Logging", () => {
2943
});
3044
});
3145

46+
describe("Formatting Error Logging", () => {
47+
it("should fall back to an extended error format when an exception occurs", () => {
48+
const error = new ExtendedError("custom message");
49+
50+
const loggedError = JSON.parse(Errors.toFormatted(error).trim());
51+
loggedError.should.have.property("errorType", "ExtendedError");
52+
loggedError.should.have.property("errorMessage", "custom message");
53+
loggedError.should.have.property("stack", ["ExtendedErrorStack"]);
54+
loggedError.should.have.property("code", 100);
55+
loggedError.should.have.property(
56+
"customProperty",
57+
"ExtendedErrorCustomProperty"
58+
);
59+
});
60+
});
61+
3262
describe("Converting an Error to a Runtime response", () => {
3363
it("should create a RuntimeErrorResponse object when an Error object is given", () => {
3464
const error = new Error("custom message");

0 commit comments

Comments
 (0)