Reporting Errors

Errors emitted from your Cloud Function are automatically captured and reported in Stackdriver Error Reporting if they produce a stack trace.

Automatically reporting errors

With the Firebase SDK for Cloud Functions this applies to any uncaught exceptions and any errors you throw that are of type Error.

For example, the following error is captured by Stackdriver Error Reporting:

export.helloError = functions.https.onRequest((request, response) => {
  // This will be reported to Stackdriver Error Reporting
  throw new Error('epic fail');
});

The following error is not captured by Stackdriver Error Reporting:

export.errorFail = functions.https.onRequest((request, response) => {
  // This will NOT be reported to Stackdriver Error Reporting
  throw 1;
});

Viewing errors

You can view reported errors in the Google Platform Console.

Manually reporting errors

To report an error to Stackdriver Error Reporting from a Cloud Function, use the Stackdriver Logging API.

Importing dependencies

From your functions directory, install the Google Stackdriver Logging Client Library for Node.js:

npm install --save @google-cloud/logging

Import the Google Cloud Client Library to access the Logging API:

const Logging = require('@google-cloud/logging');

// Instantiates a client
const logging = Logging();

Sending to Stackdriver

A properly formed log entry requires a MonitoredResource object and an ErrorEvent object.

This example reportError function demonstrates the minimum data required to report an error to Stackdriver Error Reporting.

The reportError function can be used to manually report errors, which had otherwise been caught:

User details can be passed through the ErrorContext parameter to be displayed in the Stackdriver UI and used to calculate the number of affected users.

ErrorContext can also be passed information on an HTTP Request:

export.httpError = functions.https.onRequest((request, response) => {
  const error = new Error('Test error');
  const httpRequest = httpRequest = {
    method: request.method,
    url: request.originalUrl,
    userAgent: request.get('user-agent'),
    referrer: '',
    remoteIp: request.ip
  };
  reportError(error, {httpRequest}).then(() => {
    response.end();
  });
});

Send feedback about...

Need help? Visit our support page.