aboutsummaryrefslogtreecommitdiff
path: root/src/errors
diff options
context:
space:
mode:
authorTom Harley2018-02-22 18:32:15 +0000
committerTom Harley2018-02-22 18:32:15 +0000
commit59c4e0f06b2bfb41c17111017823daa15900f94a (patch)
tree725ccb6573c5b0a1f640a43aa65eaf40a8fa0d45 /src/errors
parentMerge branch 'trh/error-middleware' into 'trh/payload-checker' (diff)
parentMerge branch 'trh/editorconfig' into 'master' (diff)
downloadmedicine-59c4e0f06b2bfb41c17111017823daa15900f94a.tar.gz
medicine-59c4e0f06b2bfb41c17111017823daa15900f94a.zip

Merge branch ‘master’ into trh/payload-checker

Diffstat (limited to 'src/errors')
-rw-r--r--src/errors/errorware.test.ts35
-rw-r--r--src/errors/errorware.ts51
2 files changed, 0 insertions, 86 deletions
diff --git a/src/errors/errorware.test.ts b/src/errors/errorware.test.ts
deleted file mode 100644
index eb85cc6..0000000
--- a/src/errors/errorware.test.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-/* tslint:disable:no-unused-expression */
-// Disabled to allow chai-style assertions.
-
-import * as chai from 'chai';
-chai.should();
-
-import { ErrorResponseBlock, RequestError } from './errorware';
-
-describe('RequestError', () => {
- describe('constructor', () => {
- it('constructs simple errors', () => {
- const err: RequestError = new RequestError(
- 404, 'user_not_found'
- );
- err.code.should.equal(404);
- err.name.should.equal('user_not_found');
- });
- it('constructs with sensible defaults', () => {
- const err: RequestError = new RequestError();
- err.code.should.not.be.undefined;
- err.name.should.not.be.undefined;
- });
- });
- describe('response generation', () => {
- it('generates responses', () => {
- const err: RequestError = new RequestError(
- 500, 'internal_server_error'
- );
- const response: ErrorResponseBlock =
- err.responseBlock();
- response.status.should.equal('error');
- response.error.should.equal('internal_server_error');
- });
- });
-});
diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts
deleted file mode 100644
index 8b7e015..0000000
--- a/src/errors/errorware.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { NextFunction, Request, Response } from 'express';
-
-export interface ErrorResponseBlock {
- status: string;
- error: string;
- error_description?: string;
- user_message?: string;
- // tslint:disable-next-line:no-any
- error_data?: any;
-}
-
-export class RequestError extends Error {
- public code: number;
- public userMessage?: string;
- // tslint:disable-next-line:no-any
- public errorData?: any;
-
- public constructor(
- errCode: number = 500,
- errName: string = 'internal_server_error',
- errDescription?: string,
- errUserMessage?: string,
- // tslint:disable-next-line:no-any
- errData?: any
- ) {
- super(errDescription);
- this.code = errCode;
- this.name = errName;
- this.userMessage = errUserMessage;
- this.errorData = errData;
- }
-
- public responseBlock(): ErrorResponseBlock {
- return {
- status: 'error',
- error: this.name,
- error_description: this.message,
- user_message: this.userMessage,
- error_data: this.errorData
- };
- }
-}
-
-type Errorware =
- (err: RequestError, req: Request, res: Response, next: NextFunction) => void;
-
-export const errorHandler: Errorware =
- (err: RequestError, req: Request, res: Response, next: NextFunction): void => {
- res.status(err.code)
- .json(err.responseBlock());
-};