Skip to content

Commit

Permalink
Merge pull request #6 from hckrnews/feature/first-draft
Browse files Browse the repository at this point in the history
Add a new field to store who is sending the error
  • Loading branch information
pieter-pon authored Jun 29, 2021
2 parents 9fc6a03 + 6b7c973 commit 79e6b29
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 66 deletions.
110 changes: 54 additions & 56 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/error",
"description": "Extended Errors",
"version": "0.2.0",
"version": "0.2.1",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/app-error.unit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/** global: describe */
import { AppError } from '../index';
import { AppError } from '../index.js';

describe('App Error test', () => {
it('It should create a app error', () => {
const error = new AppError({
value: 'test',
type: String,
message: 'Example text',
me: AppError,
});

expect(error instanceof AppError).toEqual(true);
Expand All @@ -18,13 +19,15 @@ describe('App Error test', () => {
expect(error.type).toEqual(String);
expect(error.date.constructor).toEqual(Date);
expect(error.stack.includes('AppError: Example text')).toEqual(true);
expect(error.me).toEqual(AppError);
});

it('It should handle invalid error values', () => {
const error = new AppError({
value: 'test',
type: 'string',
message: 'Example text',
me: AppError,
});

expect(error instanceof AppError).toEqual(true);
Expand All @@ -41,5 +44,6 @@ describe('App Error test', () => {
expect(error.type).toEqual(Error);
expect(error.date.constructor).toEqual(Date);
expect(error.stack.includes('AppError: Invalid error')).toEqual(true);
expect(error.me).toEqual(AppError);
});
});
2 changes: 1 addition & 1 deletion src/__tests__/not-found-error.unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** global: describe */
import { NotFoundError } from '../index';
import { NotFoundError } from '../index.js';

describe('Not Found Error test', () => {
it('It should create a not found error', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/not-implemented-error.unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** global: describe */
import { NotImplementedError } from '../index';
import { NotImplementedError } from '../index.js';

describe('Not Implemented Error test', () => {
it('It should create a not implemented error', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/server-error.unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** global: describe */
import { ServerError } from '../index';
import { ServerError } from '../index.js';

describe('Server Error test', () => {
it('It should create a server error', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/validation-error.unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** global: describe */
import { ValidationError } from '../index';
import { ValidationError } from '../index.js';

describe('Validation Error test', () => {
it('It should create a validation error', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/app-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const validator = new Validator(errorSchema);

export default (error = Error) =>
class AppError extends error {
constructor({ value = null, type = null, message }) {
constructor({ value = null, type = null, message, me = null }) {
super(message);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, AppError);
}

this.setValues({ value, type });
this.setValues({ value, type, me });
}

get name() {
Expand All @@ -27,9 +27,10 @@ export default (error = Error) =>
return this.hasErrors ? 500 : this.errorStatus;
}

setValues({ value, type }) {
setValues({ value, type, me }) {
this.value = value;
this.type = type;
this.me = me;
this.date = new Date();

if (!this.validate()) {
Expand All @@ -51,6 +52,7 @@ export default (error = Error) =>
status: this.errorStatus,
type: this.type,
date: this.date,
me: this.me,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/schemas/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export default {
status: Number,
'type?': Function,
date: Date,
};;
};

0 comments on commit 79e6b29

Please sign in to comment.