Skip to content

Commit

Permalink
Merge a2f859d into c08bcc9
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodsantunes committed Feb 25, 2019
2 parents c08bcc9 + a2f859d commit f0a7197
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 70 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -56,7 +56,7 @@ import ApplicationError, OperationalError from '@spms-apps/ts-error';
throw new ApplicationError('The message..');
// Output in catch:
{ ApplicationError: The message..
{ <ApplicationError Object>
at method a
(path a)
at method b
Expand All @@ -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..
{ <OperationalError Object>
at method a
(path a)
at method b
Expand Down
43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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",
Expand Down
14 changes: 7 additions & 7 deletions 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;
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
16 changes: 0 additions & 16 deletions 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', () => {
Expand All @@ -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));
} */
28 changes: 0 additions & 28 deletions tests/classes/error-handler.test.ts

This file was deleted.

0 comments on commit f0a7197

Please sign in to comment.