דווח על שגיאות

דיווח אוטומטי על שגיאות

אתה יכול לשדר שגיאה מפונקציית ענן לדיווח שגיאות כפי שמוצג להלן:

Node.js

// These WILL be reported to Error Reporting
throw new Error('I failed you'); // Will cause a cold start if not caught

פִּיתוֹן

@functions_framework.http
def hello_error_1(request):
    # This WILL be reported to Error Reporting,
    # and WILL NOT show up in logs or
    # terminate the function.
    from google.cloud import error_reporting

    client = error_reporting.Client()

    try:
        raise RuntimeError("I failed you")
    except RuntimeError:
        client.report_exception()

    # This WILL be reported to Error Reporting,
    # and WILL terminate the function
    raise RuntimeError("I failed you")


@functions_framework.http
def hello_error_2(request):
    # These errors WILL NOT be reported to Error
    # Reporting, but will show up in logs.
    import logging
    import sys

    print(RuntimeError("I failed you (print to stdout)"))
    logging.warning(RuntimeError("I failed you (logging.warning)"))
    logging.error(RuntimeError("I failed you (logging.error)"))
    sys.stderr.write("I failed you (sys.stderr.write)\n")

    # This is considered a successful execution and WILL NOT be reported
    # to Error Reporting, but the status code (500) WILL be logged.
    from flask import abort

    return abort(500)

אם תרצה דיווח שגיאות דק יותר, תוכל להשתמש בספריות הלקוח של דיווח שגיאות .

אתה יכול להציג את השגיאות המדווחות בדיווח שגיאות ב-GCP Console. אתה יכול גם לראות את השגיאות המדווחות מפונקציה מסוימת כאשר אתה בוחר אותה מרשימת הפונקציות ב-GCP Console.

חריגים שלא נתפסו שנוצרו על ידי הפונקציה שלך יופיעו בדיווח שגיאות. שים לב שסוגים מסוימים של חריגים שלא נתפסו (כגון אלו שנזרקו באופן אסינכרוני) יגרמו להתחלה קרה להתרחש עם הפעלת פונקציה עתידית. זה מגדיל את משך הזמן שייקח לפונקציה שלך לפעול.

דיווח ידני על שגיאות

שליחה ל-Cloud Logging

פונקציית error מ- Cloud Functions loger SDK תדווח על שגיאות הן ל-Cloud Logging והן לדיווח שגיאות . כדי לכלול יותר הקשר מהשגיאה כנתונים מובנים , העבר אובייקט שגיאה כארגומנט השני:

} catch (err) {
  // Attach an error object as the second argument
  functions.logger.error(
    "Unable to read quote from Firestore, sending default instead",
    err
  );
}