npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elysia-fault

v1.0.7

Published

You can add error middleware to APIs made with Elysia

Downloads

175

Readme

Elysia Fault

You can add error middleware to APIs made with Elysia.

Instalation

  bun add elysia-fault

Usage

It is important to remember that as it is an error handling middleware, it must be used before controllers or anything that will need to have error handling

Standard usage

import { elysiaFault, Errors } from 'elysia-fault'
import Elysia from 'elysia'

const app = new Elysia()
    .use(elysiaFault())
    .get('/', () => {
        throw new Errors.BadRequest('Example for Bad Request Error')
    })
    .listen(3333)

console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)

Output of standard usage with status code 400

{
    "name": "BadRequest",
    "message": "Example for Bad Request Error"
}

The middleware changes the NotFound and Validation standard errors

import { elysiaFault } from 'elysia-fault'
import Elysia from 'elysia'

const app = new Elysia()
    .use(elysiaFault())
    .get('/', () => 'Hello World')
    .listen(3333)

console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)

This will be the 404 error that your app will return by default when attempting a request using the http://localhost:3333/notfound route in the GET method

{
    "message": "GET http://localhost/notfound is not a valid endpoint."
}

But if you don't like the new default error, you can customize it

import { elysiaFault } from 'elysia-fault'
import Elysia from 'elysia'

const app = new Elysia()
    .use(elysiaFault({
        found: (err) => `My custom error on ${err.url} with method ${err.method}`
    }))
    .get('/', () => 'Hello World')
    .listen(3333)

console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)

With this modified error, we just told it to return a string, so it will still do it with the status code 404

"My custom error on http://localhost/notfound with method GET"

But if you like json, you can also ask it to return an object

import { elysiaFault } from 'elysia-fault'
import Elysia from 'elysia'

const app = new Elysia()
    .use(elysiaFault({
        found: (err) => ({ message: `My custom error with an object` })
    }))
    .get('/', () => 'Hello World')
    .listen(3333)

console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)

So it returns this error in a not found error

{
    "message": "My custom error with an object"
}

But if you still think you need more customization, you can create your own errors with these class extensions

import { elysiaFault, Errors } from 'elysia-fault'
import Elysia from 'elysia'

class MyCustomError extends Errors.ApiError {
    constructor(message: string) {
        super(message, 500, 'MyCustomError')
        //super(error message, status code, error name)
    }
}

const app = new Elysia()
    .use(elysiaFault())
    .get('/', () => {
        throw new MyCustomError('Custom error made by myself')
    })
    .listen(3333)

console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)

Custom error output

{
    "name": "MyCustomError",
    "message": "Custom error made by myself"
}

But if you still don't like how these custom errors are returning, you can still customize the format of these errors as well.

import { elysiaFault, Errors } from 'elysia-fault'
import Elysia from 'elysia'

const app = new Elysia()
    .use(elysiaFault({
        config: (err) => err.message
    }))
    .get('/', () => {
        throw new Errors.BadRequest('Example for Bad Request Error with only text')
    })
    .listen(3333)

console.log(`Server is running at ${app.server?.hostname}:${app.server?.port}`)

Output of standard usage with status code 400

"Example for Bad Request Error with only text"