gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:46:26 25 4
gpt4 key购买 nike

我在使用 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;
})

之后我将从我的 route 调用此函数:

 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),而不是正确的错误。

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

提前致谢!

最佳答案

不确定这有多“犹太洁食”

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

关于javascript - 在 Promise 链中抛出不同的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41764399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com