@tractor/error-handler
v1.9.4-tractor-to-playwright.0
Published
JavaScript file handler for tractor
Downloads
58
Keywords
Readme
@tractor/error-handler
A general HTTP request error handler for tractor.
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);
}
}