diff options
Diffstat (limited to 'src/errors')
| -rw-r--r-- | src/errors/errorware.test.ts | 35 | ||||
| -rw-r--r-- | src/errors/errorware.ts | 63 |
2 files changed, 62 insertions, 36 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 index b01f83b..e28e6ae 100644 --- a/src/errors/errorware.ts +++ b/src/errors/errorware.ts @@ -1,5 +1,12 @@ import { NextFunction, Request, Response } from 'express'; +/** + * General error and error-handling logic for the middleware stack. + */ + +/** + * The body of the response to a client on errors. + */ export interface ErrorResponseBlock { status: string; error: string; @@ -9,7 +16,18 @@ export interface ErrorResponseBlock { error_data?: any; } +/** + * The custom-made Error format, containing enough information to send in + * response to a client. + */ export class RequestError extends Error { + /** + * Used to check that an object is specifically a RequestError. + * + * @param test The object to test. + * @returns `true` if the object is a [[RequestError]]; `false` + * otherwise. + */ public static is(test: Object): test is RequestError { const err: RequestError = test as RequestError; return err.code !== undefined @@ -17,11 +35,36 @@ export class RequestError extends Error { && err.responseBlock !== undefined; } + /** + * The http response code associated with the error. + */ public code: number; + + /** + * The user-friendly message that describes the error. + */ public userMessage?: string; + + /** + * Miscellaneous data associated with the error. + */ // tslint:disable-next-line:no-any public errorData?: any; + /** + * Constructs a [[RequestError]] using information intended to be sent + * to the client. + * + * @param errCode The http error-code. + * @param errName The descriptive name of the problem, for further + * detail from the error-code. + * @param errDescription A more verbose description based on the error + * name. + * @param errUserMessage A user-friendly version of the description, to + * possibly be displayed to the user. + * @param errData Miscellaneous data related to the error; probably + * used more during development. + */ public constructor( errCode: number = 500, errName: string = 'internal_server_error', @@ -48,11 +91,29 @@ export class RequestError extends Error { } } +/** + * The outside shell of an Express error-handling (_4-arity_) function. + * + * @param err The Error to act on. + * @param req The Express http request. + * @param res The Express http response. + * @param next The next Express middleware function to use; probably another + * [[Errorware]]. + */ export type Errorware = (err: Error, req: Request, res: Response, next: NextFunction) => void; +/** + * A terminal [[Errorware]] for sending errors to the user. + * Has special functionality for handling [[RequestError]]s, but can handle + * regular Errors too. + * + * @param err The Error to act on. + * @param req The Express http request. + * @param res The Express http response. + */ export const errorHandler: Errorware = - (err: Error, req: Request, res: Response, next: NextFunction): void => { + (err: Error, req: Request, res: Response): void => { if (RequestError.is(err)) { res.status(err.code) .json(err.responseBlock()); |
