diff --git a/README.md b/README.md index 889dfc5..27ab283 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,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 @@ -68,16 +68,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/package-lock.json b/package-lock.json index 839ab52..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": { @@ -1876,7 +1876,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -1897,12 +1898,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1917,17 +1920,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2044,7 +2050,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2056,6 +2063,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2070,6 +2078,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2077,12 +2086,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2101,6 +2112,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2181,7 +2193,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2193,6 +2206,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2278,7 +2292,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2314,6 +2329,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2333,6 +2349,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2376,12 +2393,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, 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", 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/tests/classes/app-error.test.ts b/tests/classes/app-error.test.ts index 6aa7bb6..60dfde2 100644 --- a/tests/classes/app-error.test.ts +++ b/tests/classes/app-error.test.ts @@ -1,7 +1,5 @@ import ApplicationError from '../../src/classes/app-error'; -import { ErrorHandler } from '../../src/classes/error-handler'; import { ErrorCodes } from '../../src/common/enums/errors'; -import { IError } from '../../src/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/tests/classes/error-handler.test.ts b/tests/classes/error-handler.test.ts deleted file mode 100644 index 4de08c6..0000000 --- a/tests/classes/error-handler.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import ApplicationError from '../../src/classes/app-error'; -import { ErrorHandler } from '../../src/classes/error-handler'; -import { IError } from '../../src/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) { - const jsonStr = err.printError(); - expect(jsonStr).toBeDefined(); - expect(jsonStr).toEqual(JSON.stringify(appError)); - } - }); -});