npm.io
1.9.4-tractor-to-playwright.0 • Published 4 years ago

@tractor/error-handler

Licence
MIT
Version
1.9.4-tractor-to-playwright.0
Deps
1
Size
9 kB
Vulns
0
Weekly
0
Stars
25

@tractor/error-handler

A general HTTP request error handler for tractor.

npm version

API

new TractorError (message: string, status?: number)

Create a new TractorError.

const error = new TractorError('something bad happened', 500);
TractorError.isTractorError (err: TractorError | any): boolean

Checks if something is a TractorError.

TractorError.isTractorError(new TractorError('something bad happened')); // true;
TractorError.isTractorError(new Error('something bad happened')); // false;
handleError (response: Response, err: TractorError, message?: string): void

Sends an error back to the client, via the Express HTTP response object.

import { TractorError, handleError } from '@tractor/error-handler';

export function myApiEndpoint (request: Request, response: Response): void {
    if (somethingBad) {
        handleError(response, new TractorError('something bad happened'));
    } else {
        response.sendStatus(200);
    }
}