According to Oak GitHub page, "Middleware can be used to handle other errors with middleware. Awaiting other middleware to execute while trapping errors works. So if you had an error handling middleware that provides a well managed response to errors would work like this:”

 

import {
  Application,
  isHttpError,
  Status,
} from "https://deno.land/x/oak/mod.ts"

const app = new Application()

app.use(async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    if (isHttpError(err)) {
      switch (err.status) {
        case Status.NotFound:
          // handle NotFound
          break
        default:
          // handle other statuses
      }
    } else {
      // rethrow if you can't handle the error
      throw err
    }
  }
})