From 5dafe924bbbd5cc09901186ff74e4b68a6dcd52f Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 22 Feb 2018 18:47:08 +0000 Subject: Revert "Revert "Merge branch 'trh/error-middleware' into 'master'"" This reverts commit 3198c6c2474101623e3b95bd038effe210c176a8. --- src/errors/errorware.test.ts | 35 ++++++++++++++++++++++++++++++ src/errors/errorware.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/errors/errorware.test.ts create mode 100644 src/errors/errorware.ts (limited to 'src/errors') diff --git a/src/errors/errorware.test.ts b/src/errors/errorware.test.ts new file mode 100644 index 0000000..eb85cc6 --- /dev/null +++ b/src/errors/errorware.test.ts @@ -0,0 +1,35 @@ +/* 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 new file mode 100644 index 0000000..8b7e015 --- /dev/null +++ b/src/errors/errorware.ts @@ -0,0 +1,51 @@ +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()); +}; -- cgit v1.2.1 From 84b8b5323654cf43ef549d2151bd3206c666d9d8 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 2 Mar 2018 18:03:31 +0000 Subject: Plug in authentication support - WIP --- src/errors/errorware.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/errors') diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts index 8b7e015..45a76b3 100644 --- a/src/errors/errorware.ts +++ b/src/errors/errorware.ts @@ -42,10 +42,15 @@ export class RequestError extends Error { } type Errorware = - (err: RequestError, req: Request, res: Response, next: NextFunction) => void; + (err: Error, 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()); + (err: Error, req: Request, res: Response, next: NextFunction): void => { + if ((err as RequestError).responseBlock) { + const rerr: RequestError = err as RequestError; + res.status(rerr.code) + .json(rerr.responseBlock()); + } else { + res.send(err); + } }; -- cgit v1.2.1 From 652d328dc1d6605687cbd6858a3d27da1c126f7e Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 3 Mar 2018 16:42:16 +0000 Subject: Implement handling of unauthorised requests --- src/errors/errorware.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src/errors') diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts index 45a76b3..e2355de 100644 --- a/src/errors/errorware.ts +++ b/src/errors/errorware.ts @@ -10,6 +10,13 @@ export interface ErrorResponseBlock { } export class RequestError extends Error { + public static is(test: Object): test is RequestError { + const err: RequestError = test as RequestError; + return err.code !== undefined + && err.name !== undefined + && err.responseBlock !== undefined; + } + public code: number; public userMessage?: string; // tslint:disable-next-line:no-any @@ -41,16 +48,17 @@ export class RequestError extends Error { } } -type Errorware = +export type Errorware = (err: Error, req: Request, res: Response, next: NextFunction) => void; export const errorHandler: Errorware = (err: Error, req: Request, res: Response, next: NextFunction): void => { - if ((err as RequestError).responseBlock) { - const rerr: RequestError = err as RequestError; - res.status(rerr.code) - .json(rerr.responseBlock()); - } else { - res.send(err); - } + console.log(err); + if (RequestError.is(err)) { + console.log('fewkuihfklsfv'); + res.status(err.code) + .json(err.responseBlock()); + } else { + res.send(err); + } }; -- cgit v1.2.1