自動回報錯誤
您可以將函式中的錯誤發送至 Error Reporting,方式如下:
// These WILL be reported to Error Reporting
throw new Error('I failed you'); // Will cause a cold start if not caught
如需更精細的錯誤報告,請使用 Error Reporting 用戶端程式庫。
您可以在 Google Cloud 控制台的「Error Reporting」中查看回報的錯誤;或是在 Google Cloud 控制台的函式清單選取特定函式,查看該函式所回報的錯誤。
由函式所產生的未處理例外狀況,都會顯示在Error Reporting 中。請注意,某些類型的未處理例外狀況 (例如以非同步方式擲回的項目),會在後續函式叫用時導致冷啟動,進而增加函式執行所需的時間。
手動回報錯誤
匯入依附元件
如要從函式向 Error Reporting 回報錯誤,請從 Cloud Functions logger SDK 匯入 error 函式:
// All available logging functions
const {
log,
info,
debug,
warn,
error,
write,
} = require("firebase-functions/logger");
正在傳送至「Cloud Logging」
Cloud Functions 記錄器 SDK 的 error 函式會將錯誤回報給 Cloud Logging 和 Error Reporting。如要將錯誤的更多脈絡資訊納入結構化資料,請將錯誤物件做為第二個引數傳遞:
} catch (err) {
// Attach an error object as the second argument
error("Unable to read quote from Firestore, sending default instead",
err);