Miss any of our Open RFC calls?Watch the recordings here! »


1.0.0 • Public • Published


This package provides a decorator to use with Typescript to easily manage error (handled as unhandled). It also allow customisation for response status code and/or body response content. Warning, this package is meant for Hapi only !

Getting Started


To install this package, you need only one thing : NodeJS > 10.15.

You can use it for Typescript only.


To install, just run this command :

npm install --save sfs-error-hapi@latest


Hapi Version sfs-error-hapi
^19.0 0.9.5
^18.0 0.9.4

How to use

Basic example

You just need to decorate one of your handler like below to handle error without any customization :

import { ErrorHandler } from "sfs-error-hapi";
export class HapiHandlerClass {
  public doSomething(request: Hapi.Request, res: Hapi.ResponseToolkit): any {
    // do something

Then, use it inside one of your route like below :

const myHandler: HapiHandlerClass = new HapiHandlerClass();
const myRoute: Hapi.ServerRoute = {
  method: "get",
  path: "/normal",
  handler: myHandler.doSomething.bind(myHandler)

If your source code throw any error, then it'll be caught and sent response will look like this :

  "message": "Whatever is in the thrown message",
  "name": "Error",
  "stack": "Some technical stack\r\nI can't imagine\r\nI think you know why ;)"

Advanced usage

You can also create your own error class and customize its content. The BaseError class provides a single method .toString() that is used to send HTTP response body. You can customize it as you wish but this method is required. You can add as many attributes as you wish.

import { BaseError, ErrorHandler } from "sfs-error-hapi";
class CustomError extends BaseError {
  constructor(message?: string) {
    this.name = "CustomError";
  public toString(): string {
    return JSON.stringify({
      name: this.name
export class HapiHandlerClass {
    { code: 501, errorType: CustomError}
  public doSomething(request: Hapi.Request, res: Hapi.ResponseToolkit): any {
    // do something

If your method does throw an instance of CustomError, then the resulting HTTP status code will be 501 and the output body will look like below :

  "name": "CustomError",

Multiple error types

Of course, you can manage like above as many error types as you wish :

import { ErrorHandler } from "sfs-error-hapi";
export class HapiHandlerClass {
    { code: 501, errorType: MyCustomErrorOne },
    { code: 502, errorType: MyCustomErrorTwo },
    { code: 503, errorType: MyCustomErrorThree },
    { code: 504, errorType: MyCustomErrorFour },
    { code: 500, errorType: Error }
  public doSomething(request: Hapi.Request, res: Hapi.ResponseToolkit): any {
    // do something

Running the tests

I used Mocha and Chai to test this package. You can also get coverage thanks to NYC package (these are only used for development purposes).

This package has been tested mainly with integrations tests because its hard dependent to Hapi.

You can test package by running the following command :

npm test

And you can also get code coverage with the following command :

npm run coverage

Built With

  • Typescript - The amazing Javascript subset developed by Microsoft engineers
  • NPM - The wonderful dependency manager for Javascript
  • TsLint - A great source code linter for Typescript
  • Prettierc - An excellent source code formatter
  • Mocha - Famous unit test runner
  • Chai - Fantastic assertion library


We use GitLab for versioning.



This project is licensed under the MIT License.


npm i sfs-error-hapi

DownloadsWeekly Downloads






Unpacked Size

18.2 kB

Total Files


Last publish


  • avatar