making sure that Error from a promise is an Error object
function formatError(error: mixed): Error {
    if (typeof error === 'object' && error != null && error instanceof Error) {
        return error;
    }
    if (typeof error === 'object' && error != null && error.message != null) {
        return new Error(error.message);
    }
    return new Error(JSON.stringify(error));
}