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(); //});