在Promise链中抛出不同的错误?

时间:2017-01-20 13:03:59

标签: javascript asynchronous promise

我在使用Node的WebApp服务器中有一个方法,它将执行多个异步操作,例如:

function saveTopValuePages( pageSize, clientId  )
        setExistingCredentials(clientId)
        .then(function() {
            return getViewIdByClientId(clientId);
        }, function(error) {
            throw new customErrors.SetExistingCredentials('test123')

        })
        .then(function(viewId) {
            return fetch(viewId, options)
        }, function(error) {
            throw new customErrors.GetViewIdByClientId('abcasdasd')
        })
        .then(function(response) {
            return response;
        })

之后我将从我的路线调用此功能:

 analytics.saveTopValuePages(pageSize, clientId)
    .then(function(data) {
        res.status(200)send({ message: 'success'})
        }

    }).catch(customErrors.SetExistingCredentials, function(error) {
        res.status(400).send({ error: error})
    }).catch(customErrors.GetViewIdByClientId, function(error) {
        res.status(401).send({error: error})
    }).catch(function(e){
    res.status(500).send({ error: "Unknown internal server error" });
});

正如您在我的示例中所看到的,我正在尝试抛出错误,具体取决于进程的哪个部分失败。如果所有这些行动彼此独立,这将起作用,但事实恰恰相反。因此,如果setExistingCredentials失败,则以下所有函数都将失败。

这引发了如果setExistingCredentials失败,则会抛出错误以及GetViewIdByClientId错误。最后,我将捕获的错误将是最后一个(在这种情况下为GetViewIdByClientId),而不是正确的错误。

所以,我不确定这是否是一种正确的方法,如果有任何其他的承诺模式来实现我需要的结果。

提前致谢!

1 个答案:

答案 0 :(得分:-1)

不确定这是什么“犹太人”

function saveTopValuePages( pageSize, clientId  ) {
    setExistingCredentials(clientId)
    .then(function() {
        return getViewIdByClientId(clientId);
    })
    .catch(function(error) {
        throw { custom: customErrors, type: 'SetExistingCredentials', arg: 'test123' }

    })
    .then(function(viewId) {
        return fetch(viewId, options)
    })
    .catch(function(error) {
        if (error.custom == customErrors) {
            throw new customErrors[error.type](error.arg);
        }
        throw new customErrors.GetViewIdByClientId('abcasdasd')
    });
    // next 3 lines are actually redundant 
    //.then(function(response) {
    //    return response;
    //})
}

如果这不起作用,那么你不会喜欢它 - 我不喜欢它 - 但有时它必须完成 - 嵌套!

function saveTopValuePages( pageSize, clientId  ) {
    setExistingCredentials(clientId)
    .then(function() {
        return getViewIdByClientId(clientId).then(function(viewId) {
            return fetch(viewId, options)
        }, function(error) {
            throw new customErrors.GetViewIdByClientId('abcasdasd')
        });
    }, function(error) {
        throw new customErrors.SetExistingCredentials('test123')
    })
    .then(function(response) {
        return response;
    })
}

当然,你可以通过创建一个函数

来缓解这种肮脏
function getView(clientId) {
    return getViewIdByClientId(clientId).then(function(viewId) {
        return fetch(viewId, options)
    }, function(error) {
        throw new customErrors.GetViewIdByClientId('abcasdasd')
    });
}
function saveTopValuePages( pageSize, clientId  ) {
    setExistingCredentials(clientId)
    .then(function() {
        return getView(clientId);
    }, function(error) {
        throw new customErrors.SetExistingCredentials('test123')
    })
    .then(function(response) {
        return response;
    })
}
相关问题