2020-05-21 09:14:56 +02:00
|
|
|
import { getErrorMsg } from './helpers';
|
|
|
|
import { logger } from './logger';
|
|
|
|
|
2020-07-21 23:31:12 +02:00
|
|
|
export const to = async <T>(promise: Promise<T>) => {
|
2020-05-21 09:14:56 +02:00
|
|
|
return promise
|
|
|
|
.then(data => data)
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('%o', err);
|
|
|
|
throw new Error(getErrorMsg(err));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-21 23:31:12 +02:00
|
|
|
/*
|
|
|
|
* 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 <T>(promise: Promise<T>) => {
|
|
|
|
return promise
|
|
|
|
.then(data => [data, undefined] as const)
|
|
|
|
.catch(err => [undefined, err] as const);
|
2020-05-21 09:14:56 +02:00
|
|
|
};
|