Skip to content

Cloudflare Worker error handler made to work with Cloudflare Workers & itty-router

License

Notifications You must be signed in to change notification settings

galleu/worker-error-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Worker Error Handler

Example

import { Router } from 'itty-router';
import { handle_error } from "./handle_error"

const router = Router()


// GET index
router.get('/', () => new Response('This is the Index!'))

// GET returns 403
router.get('/403', () => {
  throw { status: 403 }
})

// GET a 404 with a custom message
router.get('/message', () => {
    throw { status: 404, message: 'This is a message that wasn\'t found.' }
})

// GET an error of your choice
router.get('/error/:status', ({ params }) => {
    throw { status: Number(params.status) }
})

// Return 404 for anything else that wasn't handled
router.all('*', () => { throw { status: 404 } });

addEventListener("fetch", (event) => {
    event.respondWith(router.handle(event.request, event).catch((err) => handle_error(err)));
});

About

Cloudflare Worker error handler made to work with Cloudflare Workers & itty-router

Topics

Resources

License

Stars

Watchers

Forks