1.1.1Updated a month ago
import { Handler } from "$fresh/server.ts";

export const handler: Handler = async (_req, ctx) => {
  let status = 200;
  try {
    const response = await ctx.next();

    status = response.status;
    if(response.status >= 400) throw new Error();

    return response;
  } catch(e: any) {
    let statusText = "It's not you - it's me";

    if(status == 401 || e instanceof Deno.errors.ConnectionRefused) {
      status = 401;
      statusText = "You shall not pass";
    }
    
    if(status == 403 || e instanceof Deno.errors.PermissionDenied) {
      status = 403;
      statusText = "Only couriers beyond this point";
    }

    if(status == 404 || e instanceof Deno.errors.NotFound) {
      status = 404;
      statusText = "These are not the packages you are looking for";
    }

    return Response.json({ error: statusText }, { status, statusText });
  }
}