Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added context and changed parameter structure to object #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,18 +15,36 @@ Logs the final object using passed function, Elastic URL or `Debug` library with
const error = new ErrorCustom(message, statusCode, errorCode, baseError, logFunction);
```

* {string} `message`
Error message to set on the Error object
* {string | IConfig} `message`
Error message to set on the Error object, or a config object for all parameters
* {number} `statusCode`
HTTP status code
HTTP status code, optional as it can be passed as part of the config
* {number} `errorCode`
The specific error code as defined in documentation
The specific error code as defined in documentation, optional as it can be passed as part of the config
* {Error} `baseError`
Optional base exception to be included as innerException property
* {Function|string} `logFunction`
Optional function to log the error with. If not supplied, debug library will be used
to log to the console with the tag `error-custom`. If a string is provided that is a
URL, it will be used to send to that URL with ElasticSearch client in Winston format.
* {object} `context`
Optional context information to provide with the error

## IConfig definition
An `Iconfig` object can be apssed instead of individual parameters as below, where the definition are the same as the constructor.
```
export interface IConfig {
message: string;
statusCode: number;
errorCode: number;
baseError?: Error;
logFunction?: string | Function;
context: any;
}
```

## toJSON
The object includes a `toJSON()` override to make all parameters iterable. Note that this returns an object, not a JSON string.

## Environment Variables
Functionality can be modified with various environment variables:
207 changes: 108 additions & 99 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@packt/error-custom",
"version": "1.2.0",
"version": "2.0.0",
"description": "Extends the JavaScript Error object with custom properties.",
"repository": {
"type": "git",
@@ -12,7 +12,7 @@
"test": "npm run lint && npm run build && npm run unit && npm run nunit",
"lint": "eslint ./src/**/*.*s",
"lint:fix": "eslint ./src/**/*.*s ./tests/**/*.*s --fix",
"unit": "mocha --require ts-node/register --require source-map-support/register --recursive 'tests/**/*.spec.*s'",
"unit": "mocha --require ts-node/register --recursive 'tests/**/*.spec.*s'",
"nunit": "nyc --check-coverage true mocha --require ts-node/register --recursive 'tests/**/*.spec.*s'",
"build": "tsc",
"prepublishOnly": "npm run test && npm run build"
@@ -44,8 +44,8 @@
"@babel/core": "^7.4.4",
"@types/chai": "^4.1.7",
"@types/debug": "^4.1.4",
"@types/joi": "^14.3.3",
"@types/mocha": "^5.2.5",
"@types/hapi__joi": "^16.0.12",
"@types/mocha": "^5.2.7",
"@types/nock": "^9.3.1",
"@types/sinon": "^7.0.13",
"@types/uuid": "^3.4.5",
@@ -68,9 +68,10 @@
},
"dependencies": {
"@elastic/elasticsearch": "^7.4.0",
"@hapi/joi": "^17.1.1",
"aws-sdk": "^2.570.0",
"debug": "4.1.1",
"joi": "14.3.1",
"source-map-support": "^0.5.16",
"uuid": "3.3.2",
"winston": "3.2.1",
"winston-elasticsearch": "0.8.2"
@@ -92,7 +93,7 @@
],
"reporter": [
"lcov",
"html"
"text"
],
"all": true
}
8 changes: 8 additions & 0 deletions src/lib/IConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface IConfig {
message: string;
statusCode: number;
errorCode: number;
baseError?: Error;
logFunction?: string | Function;
context: any;
}
103 changes: 65 additions & 38 deletions src/lib/error-custom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* eslint-disable no-param-reassign */
import debug from 'debug';
import joi from 'joi';
import joi from '@hapi/joi';
import v4 from 'uuid/v4';
import url from 'url';
import winston from 'winston';
import Elasticsearch from 'winston-elasticsearch';
import { Client } from '@elastic/elasticsearch';
// eslint-disable-next-line import/no-extraneous-dependencies
import 'source-map-support/register';
// eslint-disable-next-line no-unused-vars
import { IConfig } from './IConfig';

/**
* Error with enforced error Code and status Code and autogenerated error ID uuid.
@@ -15,6 +20,7 @@ class ErrorCustom extends Error {
public manuallyThrown: boolean;
public id: string;
public innerException: any;
public context: any;

/**
* Error Constructor
@@ -24,35 +30,50 @@ class ErrorCustom extends Error {
* Logs the final object using passed function or debug library.
*
* @constructor
* @param {string} message
* Error message to set on the Error object
* @param {string | IConfig} message
* Error message to set on the Error object, or config properties to set all params
*
* @param {number} statusCode
* HTTP status code
* HTTP status code, optional as it can be passed as part of the config
*
* @param {number} errorCode
* The specific error code as defined in documentation
* The specific error code as defined in documentation, optional as it can be passed as
* part of the config
*
* @param {Error} baseError
* @param {Error?} baseError
* Optional base exception to be included as innerException property
*
* @param {string | Function} logFunction
* Optional function to log the error with. If not supplied, debug library will be used
* to log to the console with the tag `error-custom`
*/
constructor(message: string, statusCode: number, errorCode: number,
baseError?: Error, logFunction?: string | Function) {
constructor(message: string | IConfig, statusCode?: number, errorCode?: number,
baseError?: Error, logFunction?: string | Function, context?: any) {
let messageVal: string;

// handle the case where we have a config object passed in and remap into params
if (typeof message === 'object') {
messageVal = message.message;
statusCode = message.statusCode;
errorCode = message.errorCode;
baseError = message.baseError;
logFunction = message.logFunction;
context = message.context;
} else {
messageVal = message;
}

// Validation
const messageValidation = joi.validate(message, joi.string().required());
const statusCodeValidation = joi.validate(
statusCode,
joi.number().integer().min(200).max(600)
.required(),
);
const errorCodeValidation = joi.validate(
errorCode,
joi.alternatives([joi.string(), joi.number().integer()]).required(),
);
const messageValidation = joi.string().required().validate(messageVal);
const statusCodeValidation = joi.number().integer().min(200).max(600)
.required()
.validate(
statusCode,
);
const errorCodeValidation = joi.alternatives([joi.string(), joi.number().integer()]).required()
.validate(
errorCode,
);
const errors = [];
if (messageValidation.error) errors.push('Invalid value for message parameter');
if (statusCodeValidation.error) errors.push('Invalid value for statusCode parameter');
@@ -65,34 +86,40 @@ class ErrorCustom extends Error {

// Re-enable the original chain for stack traces etc
/* istanbul ignore next */
super(message);
super(messageVal);
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
this.toJSON = this.toJSON.bind(this);
this.statusCode = statusCode;
this.errorCode = errorCode;
this.manuallyThrown = true;
this.id = v4();
this.innerException = baseError;
this.context = context;

// Log to chosen location
if (process.env.ELASTIC_LOGGING_URL) {
ErrorCustom.sendToElastic(process.env.ELASTIC_LOGGING_URL, this);
} else {
switch (typeof logFunction) {
case 'function':
logFunction(this.id, this);
break;
case 'string':
if (url.parse(logFunction).host) {
ErrorCustom.sendToElastic(logFunction, this);
try {
if (process.env.ELASTIC_LOGGING_URL) {
ErrorCustom.sendToElastic(process.env.ELASTIC_LOGGING_URL, this);
} else {
switch (typeof logFunction) {
case 'function':
logFunction(this.id, this);
break;
} else {
case 'string':
if (url.parse(logFunction).host) {
ErrorCustom.sendToElastic(logFunction, this);
break;
} else {
ErrorCustom.defaultOutput(this.id, this);
break;
}

default:
ErrorCustom.defaultOutput(this.id, this);
break;
}

default:
ErrorCustom.defaultOutput(this.id, this);
}
}
} catch (error) {
console.error('Logging attempts failed', error);
}
}

@@ -114,7 +141,7 @@ class ErrorCustom extends Error {
* @param id
* @param content
*/
private static defaultOutput(id: string, content: ErrorCustom): void {
private static defaultOutput(id: string, content: any): void {
return debug('error-custom')(id, content);
}

@@ -125,7 +152,7 @@ class ErrorCustom extends Error {
* @param content
*/
private static async sendToElastic(node: string, content: ErrorCustom): Promise<void> {
ErrorCustom.defaultOutput(content.id, content);
ErrorCustom.defaultOutput(content.id, content.toJSON());

const date = new Date();

@@ -173,7 +200,7 @@ class ErrorCustom extends Error {
esTransport,
],
});
logger.error(content.message, content);
logger.error(content.message, content.toJSON());
}
}

17 changes: 17 additions & 0 deletions tests/error-custom-built.spec.js
Original file line number Diff line number Diff line change
@@ -19,6 +19,23 @@ describe('Error Custom (Built)', () => {
expect(error.stack).to.be.not.undefined;
});

it('Constructing the instance - object version', () => {
const error = new errorCustom({
message: 'Error Message',
statusCode: 400,
errorCode: 1000,
context: { productId: '9781234567890' },
});
expect(error).to.be.instanceof(errorCustom);
expect(error.message).to.equal('Error Message');
expect(error.statusCode).to.equal(400);
expect(error.errorCode).to.equal(1000);
expect(error.manuallyThrown).to.be.true;
expect(error.stack).to.be.not.undefined;
expect(error.context).to.be.a('object');
expect(error.context.productId).to.eql('9781234567890');
});

it('undefined message fails', () => {
try {
new errorCustom(undefined, 200, 1000000);
33 changes: 25 additions & 8 deletions tests/error-custom.spec.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,23 @@ describe('Error Custom', () => {
expect(error.stack).to.be.not.undefined;
});

it('Constructing the instance - object version', () => {
const error = new ErrorCustom({
message: 'Error Message',
statusCode: 400,
errorCode: 1000,
context: { productId: '9781234567890' },
});
expect(error).to.be.instanceof(ErrorCustom);
expect(error.message).to.equal('Error Message');
expect(error.statusCode).to.equal(400);
expect(error.errorCode).to.equal(1000);
expect(error.manuallyThrown).to.be.true;
expect(error.stack).to.be.not.undefined;
expect(error.context).to.be.a('object');
expect(error.context.productId).to.eql('9781234567890');
});

it('undefined message fails', () => {
try {
new ErrorCustom(undefined, 200, 1000000);
@@ -199,7 +216,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -210,7 +227,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -223,7 +240,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -235,7 +252,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -247,7 +264,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -259,7 +276,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -271,7 +288,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});

@@ -283,7 +300,7 @@ describe('Error Custom', () => {
const errorFunc = sandbox.stub(winston, 'createLogger').returns({
error: () => { },
} as any);
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, uuid(), {});
await (ErrorCustom as any).sendToElastic(ELASTIC_LOGGING_URL, new ErrorCustom(uuid(), 500, 123455), {});
expect(errorFunc.callCount).to.be.gte(1);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
"es2017"
],
"module": "commonjs",
"target": "es5",
"target": "es2015",
"noImplicitAny": false,
"sourceMap": true,
"emitDecoratorMetadata": true,