Skip to content

zhaoworks/feh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

feh

feh is a Fastify Error Responder, it's a plugin to facilitate and standardize error management in your Fastify application.

Usage

Installation

npm install @zhaoworks/feh
yarn
yarn add @zhaoworks/feh
pnpm
pnpm add @zhaoworks/feh
bun
bun add @zhaoworks/feh

Example

After adding Fastify and feh in your project, try this

import fastify from 'fastify';
import feh from '@zhaoworks/feh';

const server = fastify();

server.register(feh);

server.get('/', (_, reply) => {
  return reply.error(500, {
    message: 'something went completely wrong >:(',
  });
});

server
  .listen({ port: 4000 })
  .then(() => console.log('Listening on http://localhost:4000/'));

You can also add a custom format to your errors by registering a new error formatter.

server.register(feh, {
  format: (status, error) => ({ error: error.message, our_fault: status === 500 })
});

server.get('/', (_, reply) => {
  return reply.error(500, { message: 'a cat must have bitten the wires' }); // -> { error: 'a cat must have bitten the wires', our_fault: true }
});