aboutsummaryrefslogtreecommitdiff
path: root/src/errors/errorware.ts
blob: 6d87bcb776fdc974b8520799696133dbf94a1949 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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;
	error_description?: string;
	user_message?: string;
	// tslint:disable-next-line:no-any
	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 {
		return (test as Partial<RequestError>).code !== undefined
			&& (test as Partial<RequestError>).name !== undefined
			&& (test as Partial<RequestError>).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',
		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
		};
	}
}

/**
 * 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.
 * @param next The next Express middleware function.
 */
export const errorHandler: Errorware = (
	err: Error, req: Request, res: Response, next: NextFunction
): void => {
	if (RequestError.is(err)) {
		res.status(err.code)
			.json(err.responseBlock());
	} else {
		res.send(err);
	}
};