Informar de errores

Puede emitir un error desde una función en la nube al informe de errores como se muestra a continuación:

// 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)

Si desea informes de errores más detallados, puede utilizar las bibliotecas cliente de Informes de errores .

Puede ver los errores informados en Informe de errores en GCP Console. También puede ver los errores informados de una función en particular cuando la selecciona de la lista de funciones en GCP Console.

Las excepciones no detectadas producidas por su función aparecerán en el Informe de errores. Tenga en cuenta que algunos tipos de excepciones no detectadas (como las lanzadas de forma asincrónica) provocarán un inicio en frío en una futura invocación de función. Esto aumenta la cantidad de tiempo que tardará su función en ejecutarse.

Informar errores manualmente

Importando dependencias

Para informar un error a Error Reporting desde una función, importe la función error desde el SDK del registrador de Cloud Functions:

// All available logging functions
const {
  log
,
  info
,
  debug
,
  warn
,
  error
,
  write
,
} = require("firebase-functions/logger");

Envío a registro en la nube

La función error del SDK del registrador de Cloud Functions informará los errores tanto a Cloud Logging como a Error Reporting . Para incluir más contexto del error como datos estructurados , pase un objeto de error como segundo argumento:

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