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
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -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:
Expand Down
33 changes: 25 additions & 8 deletions package-lock.json

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

9 changes: 5 additions & 4 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",
Expand All @@ -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"
Expand Down Expand Up @@ -45,7 +45,7 @@
"@types/chai": "^4.1.7",
"@types/debug": "^4.1.4",
"@types/joi": "^14.3.3",
"@types/mocha": "^5.2.5",
"@types/mocha": "^5.2.7",
"@types/nock": "^9.3.1",
"@types/sinon": "^7.0.13",
"@types/uuid": "^3.4.5",
Expand All @@ -71,6 +71,7 @@
"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"
Expand All @@ -92,7 +93,7 @@
],
"reporter": [
"lcov",
"html"
"text"
],
"all": true
}
Expand Down
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;
}
47 changes: 35 additions & 12 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 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.
Expand All @@ -15,6 +20,7 @@ class ErrorCustom extends Error {
public manuallyThrown: boolean;
public id: string;
public innerException: any;
public context: any;

/**
* Error Constructor
Expand All @@ -24,26 +30,41 @@ 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 messageValidation = joi.validate(messageVal, joi.string().required());
const statusCodeValidation = joi.validate(
statusCode,
joi.number().integer().min(200).max(600)
Expand All @@ -65,13 +86,15 @@ 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) {
Expand Down Expand Up @@ -114,7 +137,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);
}

Expand All @@ -125,7 +148,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();

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

Expand Down
17 changes: 17 additions & 0 deletions tests/error-custom-built.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 25 additions & 8 deletions tests/error-custom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});
});
Expand Down
Loading