Skip to content

Commit

Permalink
Fix errors issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Botez authored and edhzsz committed Jan 24, 2021
1 parent 2a62749 commit e5d6234
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/Errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ export const toFormatted = (error: unknown): string => {
* user-provided enumerable properties.
*/
function _withEnumerableProperties(error: any) {
if (error instanceof ExtendedError) {
if (error instanceof Error) {
const extendedError: ExtendedError = <ExtendedError>(<any>error);
const ret: any = Object.assign(
{
errorType: error.name,
errorMessage: error.message,
code: error.code,
errorType: extendedError.name,
errorMessage: extendedError.message,
code: extendedError.code,
},
error
extendedError
);
if (typeof error.stack == "string") {
ret.stack = error.stack.split("\n");
if (typeof extendedError.stack == "string") {
ret.stack = extendedError.stack.split("\n");
}
return ret;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/UserFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function _loadUserApp(
return _tryRequire(appRoot, moduleRoot, module);
} catch (e) {
if (e instanceof SyntaxError) {
throw new UserCodeSyntaxError(e.message);
throw new UserCodeSyntaxError(<any>e);
} else if (e.code !== undefined && e.code === "MODULE_NOT_FOUND") {
throw new ImportModuleError(e);
} else {
Expand Down
32 changes: 31 additions & 1 deletion test/unit/Errors/Errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ class CircularError extends Error {
}
}

describe("Formatting Error Logging", () => {
class ExtendedError extends Error {
code?: number;
customProperty?: string;

constructor(message?: string) {
super(message);

this.name = "ExtendedError";
this.stack = "ExtendedErrorStack";
this.code = 100;
this.customProperty = "ExtendedErrorCustomProperty";
}
}

describe("Formatting CircularError Logging", () => {
it("should fall back to a minimal error format when an exception occurs", () => {
const error = new CircularError("custom message");
error.backlink = error;
Expand All @@ -29,6 +43,22 @@ describe("Formatting Error Logging", () => {
});
});

describe("Formatting Error Logging", () => {
it("should fall back to an extended error format when an exception occurs", () => {
const error = new ExtendedError("custom message");

const loggedError = JSON.parse(Errors.toFormatted(error).trim());
loggedError.should.have.property("errorType", "ExtendedError");
loggedError.should.have.property("errorMessage", "custom message");
loggedError.should.have.property("stack", ["ExtendedErrorStack"]);
loggedError.should.have.property("code", 100);
loggedError.should.have.property(
"customProperty",
"ExtendedErrorCustomProperty"
);
});
});

describe("Converting an Error to a Runtime response", () => {
it("should create a RuntimeErrorResponse object when an Error object is given", () => {
const error = new Error("custom message");
Expand Down

0 comments on commit e5d6234

Please sign in to comment.