From 04dac0ac44c764015ecfe3695e09d67c130b5250 Mon Sep 17 00:00:00 2001 From: Pedro Antunes Date: Wed, 20 Feb 2019 16:16:28 +0000 Subject: [PATCH 1/3] feat(*): change print error to toString --- README.md | 13 ++++++----- src/classes/error-handler.ts | 14 ++++++------ src/tests/classes/app-error.test.ts | 16 ------------- src/tests/classes/error-handler.test.ts | 30 ------------------------- 4 files changed, 14 insertions(+), 59 deletions(-) delete mode 100644 src/tests/classes/error-handler.test.ts diff --git a/README.md b/README.md index 0ab8251..534ebce 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ import ApplicationError, OperationalError from '@spms-apps/ts-error'; throw new ApplicationError('The message..'); // Output in catch: -{ ApplicationError: The message.. +{ at method a (path a) at method b @@ -65,16 +65,17 @@ throw new ApplicationError('The message..'); uid: '0A', timestamp: 2019-01-31T16:05:23.700Z, stackMsg: - [ 'ApplicationError: Error 1', - at method a - (path a) + [ at method a + (path pa), at method b - (path b) ], + (path pb), + at method c + (path pc)], code: 600 } throw new OperationalError('Http Bad request error message..', 400); // Output in catch: -{ OperationalError: Http Bad request error message.. +{ at method a (path a) at method b diff --git a/src/classes/error-handler.ts b/src/classes/error-handler.ts index 8a89e41..fbb2e3a 100644 --- a/src/classes/error-handler.ts +++ b/src/classes/error-handler.ts @@ -1,4 +1,5 @@ import { v4 } from 'uuid'; +import { IError } from '../interfaces/error-handling'; export abstract class ErrorHandler extends Error { protected uid: string; @@ -10,14 +11,13 @@ export abstract class ErrorHandler extends Error { public constructor(message: string) { super(message); this.name = this.constructor.name; - this.message = message; Error.captureStackTrace(this, this.constructor); this.uid = v4(); this.timestamp = new Date(); - this.stackMsg = this.getErrorStack(this.stack); + this.stackMsg = this.getErrorStack(); } - public printError(): string { + public toString(): string { return JSON.stringify({ uid: this.uid, timestamp: this.timestamp, @@ -26,12 +26,12 @@ export abstract class ErrorHandler extends Error { stack: this.stackMsg, code: this.code, statusCode: this.statusCode, - }); + } as IError); } - private getErrorStack(stack: string | undefined): string[] { - if (stack) { - const filteredErrors = stack.split(/\r?\n/).slice(0, 3); + private getErrorStack(): string[] { + if (this.stack) { + const filteredErrors = this.stack.split(/\r?\n/).slice(1, 4); const result = filteredErrors.map((str: string) => str.trim()); return result; } else { diff --git a/src/tests/classes/app-error.test.ts b/src/tests/classes/app-error.test.ts index a63ff69..800e18f 100644 --- a/src/tests/classes/app-error.test.ts +++ b/src/tests/classes/app-error.test.ts @@ -1,7 +1,5 @@ import ApplicationError from '../../classes/app-error'; -import { ErrorHandler } from '../../classes/error-handler'; import { ErrorCodes } from '../../common/enums/errors'; -import { IError } from '../../interfaces/error-handling'; describe('App Error Class - Test', () => { it('Constructor test', () => { @@ -19,17 +17,3 @@ describe('App Error Class - Test', () => { } }); }); - -/* it('Undefined stack test', () => { - try { - Error.captureStackTrace - Error.prototype.stack = jest.fn().mockReturnValueOnce(undefined); - console.log(new AppError('').stack); - const app = new AppError('Error 1'); - console.log(app.stack); - } catch (err) { - // console.log(err.stack); - console.log(err); - // expect(jsonStr).toBeDefined(s); - // expect(jsonStr).toEqual(JSON.stringify(appError)); - } */ diff --git a/src/tests/classes/error-handler.test.ts b/src/tests/classes/error-handler.test.ts deleted file mode 100644 index 64290e4..0000000 --- a/src/tests/classes/error-handler.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import ApplicationError from '../../classes/app-error'; -import { ErrorHandler } from '../../classes/error-handler'; -import { IError } from '../../interfaces/error-handling'; - -describe('Print Error - Test', () => { - it('PrintError test', () => { - const d = new Date(); - const appError: IError = { - uid: 'A0', - timestamp: d, - name: 'ApplicationError', - message: 'Error 1', - stack: [], - code: 600, - }; - try { - ErrorHandler.prototype.printError = jest.fn().mockImplementationOnce(() => { - return JSON.stringify(appError); - }); - const errorClass = new ApplicationError('Error 1'); - throw errorClass; - } catch (err) { - // console.log(err); - const jsonStr = err.printError(); - console.log(jsonStr); - expect(jsonStr).toBeDefined(); - expect(jsonStr).toEqual(JSON.stringify(appError)); - } - }); -}); From 934300e4b53d88ca92448db0ddcd97671c4f7808 Mon Sep 17 00:00:00 2001 From: Pedro Antunes Date: Wed, 20 Feb 2019 16:43:56 +0000 Subject: [PATCH 2/3] fix(*): change import to single quotes --- tests/classes/app-error.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/classes/app-error.test.ts b/tests/classes/app-error.test.ts index 6cfa9b9..60dfde2 100644 --- a/tests/classes/app-error.test.ts +++ b/tests/classes/app-error.test.ts @@ -1,5 +1,5 @@ -import ApplicationError from "../../src/classes/app-error"; -import { ErrorCodes } from "../../src/common/enums/errors"; +import ApplicationError from '../../src/classes/app-error'; +import { ErrorCodes } from '../../src/common/enums/errors'; describe('App Error Class - Test', () => { it('Constructor test', () => { From a2f859d8a05a21b1d9cd445acab503946663884f Mon Sep 17 00:00:00 2001 From: Pedro Antunes Date: Mon, 25 Feb 2019 12:15:32 +0000 Subject: [PATCH 3/3] 0.0.5 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7b9802c..1ac9238 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@spms-apps/ts-error", - "version": "0.0.4", + "version": "0.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8e79dac..4904b03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spms-apps/ts-error", - "version": "0.0.4", + "version": "0.0.5", "description": "Package to throw custom errors.", "main": "lib/index.js", "types": "lib/index.d.ts",