Skip to content

Commit

Permalink
Handle middleware & routeHandler errors separately
Browse files Browse the repository at this point in the history
  • Loading branch information
rikilele committed Jul 20, 2021
1 parent 837ae4c commit 334ff6d
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/Kyuko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ export class Kyuko {
const { pathname, searchParams } = new URL(req.url);

// Handle routing
let handler: KyukoRouteHandler = this.#defaultHandler;
let routeHandler: KyukoRouteHandler = this.#defaultHandler;
const routePath = this.#routes.findMatch(pathname);
if (routePath !== undefined) {
const customHandlers = this.#customHandlers.get(req.method);
if (customHandlers?.has(routePath)) {
handler = customHandlers.get(routePath) as KyukoRouteHandler;
routeHandler = customHandlers.get(routePath) as KyukoRouteHandler;
}

// Fill req.params
Expand All @@ -221,44 +221,49 @@ export class Kyuko {
req.query.append(key, value);
});

this.invokeHandlers(req, res, handler);
this.invokeHandlers(req, res, routeHandler);
}

private async invokeHandlers(
req: KyukoRequest,
res: KyukoResponse,
handler: KyukoRouteHandler,
routeHandler: KyukoRouteHandler,
) {
// Run middleware and route handler
// Run middleware
try {
for (const middleware of this.#middleware) {
await middleware(req, res);
}
} catch (err) {
console.error(brightRed("Error in KyukoMiddleware:"));
console.error(err);
this.handleError(err, req, res);
}

// Run route handler
try {
if (!res.wasSent()) {
handler(req, res);
await routeHandler(req, res);
}
} catch (err) {
console.error(brightRed("Error in KyukoRouteHandler:"));
console.error(err);
this.handleError(err, req, res);
}
}

// Catch error from middleware OR route handler
} catch (err1) {
console.error(brightRed("Error in KyukoMiddleware / KyukoRouteHandler:"));
console.error(err1);

// Run error handlers
try {
for (const errorHandler of this.#errorHandlers) {
await errorHandler(err1, req, res);
}

// Catch error from error handler
} catch (err2) {
console.error(brightRed("Error in KyukoErrorHandler:"));
console.error(err2);
private async handleError(err: Error, req: KyukoRequest, res: KyukoResponse) {
try {
for (const errorHandler of this.#errorHandlers) {
await errorHandler(err, req, res);
}
} catch (ohShit) {
console.error(brightRed("Error in KyukoErrorHandler:"));
console.error(ohShit);
}

if (!res.wasSent()) {
res.status(500).send();
}
if (!res.wasSent()) {
res.status(500).send();
}
}
}

0 comments on commit 334ff6d

Please sign in to comment.