import { getErrorMsg } from './helpers'; import { logger } from './logger'; export const to = async (promise: Promise) => { return promise .then(data => data) .catch(err => { logger.error('%o', err); throw new Error(getErrorMsg(err)); }); }; /* * This is hard/impossible to type correctly. What we are describing * here is a set of two states: either we have a result and no error, * _or_ we have no result and an error. Unfortunately TypeScript is * not able to infer this correctly... * https://github.com/microsoft/TypeScript/issues/12184 */ export const toWithError = async (promise: Promise) => { return promise .then(data => [data, undefined] as const) .catch(err => [undefined, err] as const); };