import Knex from 'knex'; export type EndpointHandler = (req: any, res: any, knex: Knex) => Promise; export interface EndpointError { internalMessage: String; httpStatus: Number; } export function isEndpointError(obj: any): obj is EndpointError { return obj.internalMessage !== undefined && obj.httpStatus !== undefined; } export const catchUnhandledErrors = (_e: any) => { if (isEndpointError(_e)) { // Rethrow throw _e; } // This is an unhandled error, make an internal server error out of it. const e: EndpointError = { internalMessage: _e, httpStatus: 500 } throw e; }