UNPKG

1.52 kBPlain TextView Raw
1import { BaseController } from "./base.controller";
2import Request from "./request";
3import Response from "./response";
4import { isProduction } from "./app";
5
6/**
7 * The error controller is a custom controller that is invoked when an error
8 * occurred.
9 * It is invoked only on non-API routes (on API routes, the standard API error is used.)
10 */
11export default class ErrorController extends BaseController {
12 /**
13 * This method will be executed when a route is not found (the tipical 404
14 * error).
15 * @param req the standard lynx Request
16 */
17 public async onNotFound(req: Request): Promise<Response> {
18 if (req.lynx && req.lynx.route && req.lynx.route.isAPI) {
19 throw this.error(404, 'not found');
20 }
21 return this.render("lynx/404", req);
22 }
23
24 /**
25 * This method will be executed when an error is thrown during the execution
26 * of a route.
27 * @param error The error, with an additional "statusCode" property, containing the http status code.
28 * @param req the standard lynx Request
29 */
30 public async onError(error: Error, req: Request): Promise<Response> {
31 let e = error as any;
32 if (!e.statusCode) {
33 e.statusCode = 500;
34 }
35 if (req.lynx && req.lynx.route && req.lynx.route.isAPI) {
36 throw this.error(e.statusCode, error.message);
37 }
38 return this.render("lynx/error", req, {
39 error: error,
40 isProduction: isProduction()
41 });
42 }
43}