@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'));
TractorError.isTractorError(new Error('something bad happened'));
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);
}
}