我想知道當我有鏈接承諾時編寫node.js代碼的正確方法是什麼,如果出現錯誤,我需要更新實時數據庫?用於Firebase錯誤處理的雲端函數
下面是代碼:
export const testErrorHandling = functions.database
.ref('/workqueue/{pushId}/something').onWrite(event => {
// Exit when the data is deleted.
if (!event.data.exists()) {
return;
}
//This is the retry count, give up if more than 5 times have been retried.
const data = event.data.val()
if (data.count >= 5) {
return
}
return event.data.ref.root.child(data.fulluri).once('value').then(snapshot => {
//Process all, if ok, delete the work queue entry
return event.data.ref.remove()
}).catch(exception => {
console.log('Error!: ' + exception)
//Log error, increase retry count by one an write to that
//location to trigger a retry
//Is the line below OK?
//return event.data.ref.child('count').set(data.count + 1)
})
})
我想這是在許多情況下常見的需求,但是因爲所有的例子似乎只是寫console.error並做不能找到一個例子。 (在現實世界很少。)
只有在代碼中實際處理它們時,才應該發現錯誤。那麼你期望出現什麼問題會引發這個問題呢?你想怎麼處理它? –
順便說一句:目前沒有辦法強制在雲端函數中重試,但它可能會在未來版本中添加。 –
我們正在AWS Lambda上創建一個PDF,將其寫入雲存儲,確保其寫入正確並可以打開,使用包含書面PDF作爲附件的郵戳發送電子郵件並相應地更新數據。 我們可以設置重試所有的https調用等,但工作量和測試有點太多。編寫idempodent代碼(它必須是不管如何實現重試)並重試整個過程要容易得多。 因此,通常我們看到服務停止運行,導致臨時錯誤代碼或最終導致雲存儲寫入損壞 – anotherdev