Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

elysia-http-errors

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elysia-http-errors

Errors handler for ElysiaJS

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Elysia HTTP Errors

Errors handler for ElysiaJS

1. Installation

bun add elysia-http-errors

2. Usage

[!NOTE]
Must be used before controllers or anything that will need to have error handling.

import { httpErrors, BadRequest } from "elysia-http-errors"
import Elysia from "elysia"

const app = new Elysia()
	.use(
		httpErrors(
			{
				LoginRequired: (code: number) => ({
					message: "Please login before request this.",
					status: code,
				}),
			},
			{
				custom: ({ error }) => ({
					name: error.name,
					message: error.message,
					code: error.status,
				}),
			},
		),
	)
	.get("/", ({ createError }) => {
		return createError("LoginRequired", 401)
	})
    .get("/test", () => {
		throw new BadRequest('This is a bad request.')
	})

app.listen(5500, (server) => {
	console.log(`> Server is running at: ${server.url.origin}`)
})

[!NOTE]
Alway return createError when you want to create error.

// Correct:
app.get("/", ({ createError }) => {
	return createError("LoginRequired", 401)
})
// Not this:
app.get("/", ({ createError }) => {
	createError("LoginRequired", 401)
})

3. Customize Errors.

  • Need more customiziation? You can extends APIError class.
import { APIError, httpError } from "elysia-http-errors"
import Elysia from "elysia"

export class CustomError extends APIError {
    constructor(message: string) {
        super(message, 400, 'YourCustomError')
    }
}

const app = new Elysia()
    .use(
		httpErrors(
			{
				CustomError: () => {
                    throw new CustomError('CustomMessage')
                }
			},
			{
				custom: ({ error }) => ({
					name: error.name,
					message: error.message,
					code: error.status,
				}),
			},
		),
	)
    .get("/", ({ createError }) => {
		return createError("CustomError")
	})
    .get("/test", () => {
		throw new CustomError('This is a bad request.')
	})

Keywords

FAQs

Package last updated on 26 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc