From 3a6315f618935783f3357c74683aa6ee26da20c7 Mon Sep 17 00:00:00 2001 From: kbariotis Date: Mon, 21 Dec 2020 14:01:13 +0200 Subject: [PATCH 1/4] add JSON serialization --- lib/CustomError.ts | 25 +++++++++++++++++++++++++ tests/CustomError.spec.ts | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/lib/CustomError.ts b/lib/CustomError.ts index 9834f31..7d66d5d 100644 --- a/lib/CustomError.ts +++ b/lib/CustomError.ts @@ -23,4 +23,29 @@ export default class CustomError extends Error { this.stack = new Error(message).stack; } } + + toJSON() { + const jsonStructure = { + message: this.message, + statusCode: this.statusCode, + errorCode: this.errorCode, + name: this.name, + stacktrace: this.stack, + originalError: {}, + }; + + if (this.originalError) { + if ("toJSON" in this.originalError) { + jsonStructure.originalError = (this.originalError as any).toJSON(); + } else { + jsonStructure.originalError = { + name: this.originalError.name, + message: this.originalError.message, + stacktrace: this.originalError.stack, + }; + } + } + + return jsonStructure; + } } diff --git a/tests/CustomError.spec.ts b/tests/CustomError.spec.ts index d0a9223..9266f30 100644 --- a/tests/CustomError.spec.ts +++ b/tests/CustomError.spec.ts @@ -40,4 +40,8 @@ try { // The customer error code should have been set assert.strictEqual(err.errorCode, 4000); + assert.strictEqual(err.toJSON().message, "It went bad!"); + assert.strictEqual(err.toJSON().statusCode, 400); + assert.strictEqual(err.toJSON().errorCode, 4000); + assert.strictEqual(err.toJSON().name, "CustomError"); } From d40a0eda1673a07a60adf37b098838adabaceab6 Mon Sep 17 00:00:00 2001 From: kbariotis Date: Tue, 22 Dec 2020 08:56:59 +0200 Subject: [PATCH 2/4] better structure def --- lib/CustomError.ts | 20 ++++++++++++++++---- tests/CustomError.spec.ts | 2 ++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/CustomError.ts b/lib/CustomError.ts index 7d66d5d..8774ab9 100644 --- a/lib/CustomError.ts +++ b/lib/CustomError.ts @@ -1,3 +1,16 @@ +type JSONStruct = { + message: string; + statusCode: number; + errorCode: number; + name: string; + stack?: string; + originalError?: { + message: string; + name: string; + stack?: string; + }; +}; + export default class CustomError extends Error { errorCode: number; statusCode: number; @@ -25,13 +38,12 @@ export default class CustomError extends Error { } toJSON() { - const jsonStructure = { + const jsonStructure: JSONStruct = { message: this.message, statusCode: this.statusCode, errorCode: this.errorCode, name: this.name, - stacktrace: this.stack, - originalError: {}, + stack: this.stack, }; if (this.originalError) { @@ -41,7 +53,7 @@ export default class CustomError extends Error { jsonStructure.originalError = { name: this.originalError.name, message: this.originalError.message, - stacktrace: this.originalError.stack, + stack: this.originalError.stack, }; } } diff --git a/tests/CustomError.spec.ts b/tests/CustomError.spec.ts index 9266f30..6413f11 100644 --- a/tests/CustomError.spec.ts +++ b/tests/CustomError.spec.ts @@ -44,4 +44,6 @@ try { assert.strictEqual(err.toJSON().statusCode, 400); assert.strictEqual(err.toJSON().errorCode, 4000); assert.strictEqual(err.toJSON().name, "CustomError"); + assert.notStrictEqual(err.toJSON().originalError, undefined); + assert.notStrictEqual(err.toJSON().stack, undefined); } From b2d18429c72f5b5732a25900a63147b50e043b76 Mon Sep 17 00:00:00 2001 From: kbariotis Date: Tue, 22 Dec 2020 08:58:37 +0200 Subject: [PATCH 3/4] refine type --- lib/CustomError.ts | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/CustomError.ts b/lib/CustomError.ts index 8774ab9..9bc282f 100644 --- a/lib/CustomError.ts +++ b/lib/CustomError.ts @@ -1,16 +1,3 @@ -type JSONStruct = { - message: string; - statusCode: number; - errorCode: number; - name: string; - stack?: string; - originalError?: { - message: string; - name: string; - stack?: string; - }; -}; - export default class CustomError extends Error { errorCode: number; statusCode: number; @@ -38,7 +25,18 @@ export default class CustomError extends Error { } toJSON() { - const jsonStructure: JSONStruct = { + const jsonStructure: { + message: string; + statusCode: number; + errorCode: number; + name: string; + stack?: string; + originalError?: { + message: string; + name: string; + stack?: string; + }; + } = { message: this.message, statusCode: this.statusCode, errorCode: this.errorCode, @@ -47,15 +45,11 @@ export default class CustomError extends Error { }; if (this.originalError) { - if ("toJSON" in this.originalError) { - jsonStructure.originalError = (this.originalError as any).toJSON(); - } else { - jsonStructure.originalError = { - name: this.originalError.name, - message: this.originalError.message, - stack: this.originalError.stack, - }; - } + jsonStructure.originalError = { + name: this.originalError.name, + message: this.originalError.message, + stack: this.originalError.stack, + }; } return jsonStructure; From 1199ed87a867e91624a1236629b8c5263728c2b7 Mon Sep 17 00:00:00 2001 From: kbariotis Date: Tue, 22 Dec 2020 09:00:16 +0200 Subject: [PATCH 4/4] add ocmment --- tests/CustomError.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CustomError.spec.ts b/tests/CustomError.spec.ts index 6413f11..ce764fd 100644 --- a/tests/CustomError.spec.ts +++ b/tests/CustomError.spec.ts @@ -38,7 +38,7 @@ try { // The response error code should have been set assert.strictEqual(err.statusCode, 400); - // The customer error code should have been set + // Serializes JSON structure assert.strictEqual(err.errorCode, 4000); assert.strictEqual(err.toJSON().message, "It went bad!"); assert.strictEqual(err.toJSON().statusCode, 400);