繁体   English   中英

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

[英]Throw different errors in Promise chain?

我在使用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 ,如果setExistingCredentials失败,则将引发该错误以及GetViewIdByClientId错误。 最后,我要捕获的错误将是最后一个错误(在本例中为GetViewIdByClientId ),而不是正确的错误。

因此,我不确定这是否是正确的方法,是否有其他承诺模式来实现所需的结果,我不确定。

提前致谢!

不知道这是多么“洁净”

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;
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM