sfs-error-hapi v2.0.0
SFS-Error-Hapi
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
Prerequisites
To install this package, you need only one thing : NodeJS > 10.15.
You can use it for Typescript only.
Installing
To install, just run this command :
npm install --save sfs-error-hapi@latest
Compatibility
Hapi Version | sfs-error-hapi | Documentation |
---|---|---|
^20.0 | 2.0.0 | That's on this page |
^19.0 | 1.0.0 | Here |
^18.0 | 0.9.4 | Here |
How to use
Basic example
You just need to decorate one of your handler like below to handle error without any customization :
import Hapi from "@hapi/hapi";
import { ErrorHandler } from "sfs-error-hapi";
export class HapiHandlerClass {
@ErrorHandler()
public doSomething(request: Hapi.Request, res: Hapi.ResponseToolkit): any {
// do something
}
}
Then, use it inside one of your route like below :
import Hapi from "@hapi/hapi";
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 Hapi from "@hapi/hapi";
import { BaseError, ErrorHandler } from "sfs-error-hapi";
class CustomError extends BaseError {
constructor(message?: string) {
super(message);
this.name = "CustomError";
}
public toString(): string {
return JSON.stringify({
name: this.name
});
}
}
export class HapiHandlerClass {
@ErrorHandler([
{ 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 Hapi from "@hapi/hapi";
import { ErrorHandler } from "sfs-error-hapi";
export class HapiHandlerClass {
@ErrorHandler([
{ 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
}
}
Logging errors
You may want to log error somehow. Added with v1.1.0 only, you can also provide a logger to the ErrorHandler
with a method .error(message: any): void
which will log the caught error.
For example, imagine you have your own logger class :
import Hapi from "@hapi/hapi";
import { ErrorHandler, Logger } from "sfs-error-hapi";
export class MyLogger implements Logger {
/* Method from the interface Logger exported by sfs-error-hapi package */
public error(message: any): void {
console.error(message)
}
}
const logger: Logger = new Logger();
export class HapiHandlerClass {
@ErrorHandler([{ 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
- ESLint - A great source code linter for Javascript & Typescript
- Prettierc - An excellent source code formatter
- Mocha - Famous unit test runner
- Chai - Fantastic assertion library
- SinonJS - Amazing mock library
Versioning
We use GitLab for versioning.
Authors
- Sébastien FRANCOIS - Initial work - @francois.sebastien.emile
License
This project is licensed under the MIT License.