From a208c40e06a54e6a17bb094960b8890bec3e7627 Mon Sep 17 00:00:00 2001 From: monoid Date: Mon, 12 Jun 2023 10:36:33 +0900 Subject: [PATCH] feat: ignore error --- app.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app.ts b/app.ts index 2814743..3c2a9d6 100644 --- a/app.ts +++ b/app.ts @@ -1,4 +1,4 @@ -import { Application, Router } from "https://deno.land/x/oak@v12.1.0/mod.ts"; +import { Application, Router, isHttpError } from "https://deno.land/x/oak@v12.1.0/mod.ts"; import { searchRepositoryWithTopic, getRepositoryTags, @@ -123,13 +123,29 @@ router.get("/:package([a-z0-9_]*@[a-z0-9_]*)/:version?/:path*", async (ctx) => { } }); - app.use(router.routes()); app.use(router.allowedMethods()); +app.use(async (ctx, next) => { + try{ + await next(); + } + catch (err) { + console.log(err); + if (isHttpError(err)) { + ctx.response.status = err.status; + const { message, status, stack } = err; + ctx.response.body = { message, status, stack }; + } + else { throw err; + } + } +}); + + + //app.use(async (ctx, next) => { // ctx.throw(404); -// //ctx.response.status = 404; // //ctx.response.body = "Not Found"; // //await next(); //});