From e5d62346eb08f1723348197df70d4f55f9c2ebcf Mon Sep 17 00:00:00 2001 From: Stefan Botez Date: Fri, 8 Jan 2021 14:09:00 +0000 Subject: [PATCH] Fix errors issues --- src/Errors/index.ts | 15 ++++++++------- src/utils/UserFunction.ts | 2 +- test/unit/Errors/Errors.test.ts | 32 +++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/Errors/index.ts b/src/Errors/index.ts index 275a339..8d47aeb 100644 --- a/src/Errors/index.ts +++ b/src/Errors/index.ts @@ -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 = (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 { diff --git a/src/utils/UserFunction.ts b/src/utils/UserFunction.ts index 7adc042..cf39e07 100644 --- a/src/utils/UserFunction.ts +++ b/src/utils/UserFunction.ts @@ -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(e); } else if (e.code !== undefined && e.code === "MODULE_NOT_FOUND") { throw new ImportModuleError(e); } else { diff --git a/test/unit/Errors/Errors.test.ts b/test/unit/Errors/Errors.test.ts index 662ad9c..e2841ef 100644 --- a/test/unit/Errors/Errors.test.ts +++ b/test/unit/Errors/Errors.test.ts @@ -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; @@ -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");