-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerErrorRegistry.ts
More file actions
52 lines (44 loc) · 1.42 KB
/
ServerErrorRegistry.ts
File metadata and controls
52 lines (44 loc) · 1.42 KB
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
import {Response} from "./response/Response.js";
import {TextResponse} from "./response/TextResponse.js";
/**
* A registry for server errors.
*/
class ServerErrorRegistry {
private readonly responses: Record<ServerErrorRegistry.ErrorCodes, Response>;
/**
* Create a new server error registry initialised with default responses.
*/
public constructor() {
this.responses = {
[ServerErrorRegistry.ErrorCodes.BAD_URL]:
new TextResponse("Bad request URL.", 400),
[ServerErrorRegistry.ErrorCodes.NO_ROUTE]:
new TextResponse("No route in this registry matches the request.", 404),
[ServerErrorRegistry.ErrorCodes.INTERNAL]:
new TextResponse("An internal error occurred.", 500),
};
}
/**
* Replace server error response by registering a new custom response.
* @param code The server error code.
* @param response The response to send.
*/
public register(code: ServerErrorRegistry.ErrorCodes, response: Response) {
this.responses[code] = response;
}
/** @internal */
public _get(code: ServerErrorRegistry.ErrorCodes): Response {
return this.responses[code];
}
}
namespace ServerErrorRegistry {
/**
* Server error codes
*/
export const enum ErrorCodes {
BAD_URL,
NO_ROUTE,
INTERNAL,
}
}
export {ServerErrorRegistry};