Reporting errors

This tutorial shows how to report and track uncaught exceptions in the Python Bookshelf Application using Google Stackdriver Error Reporting.

Stackdriver Error Reporting aggregates errors in your application. It provides a centralized dashboard showing counts of each unique error, stack traces, and a history of occurences. You can also set up alerting for when new errors occur.

This page is part of a multipage tutorial. To start from the beginning and read the setup instructions, go to Python Bookshelf app.

Configuring settings

This section uses code in the 5-logging directory. Edit the files and run commands in this directory.

  1. Open for editing.
  2. Set the value of PROJECT_ID to your project ID, which is visible in the GCP Console.
  3. Set DATA_BACKEND to the same value you used during the Using Structured Data tutorial.
  4. If you are using Cloud SQL or MongoDB, set the values under the Cloud SQL or Mongo section to the same values you used during the Using Structured Data step.
  5. Set the value CLOUD_STORAGE_BUCKET to your Cloud Storage bucket name.

  6. Under the OAuth2 configuration section, set the values of GOOGLE_OAUTH2_CLIENT_ID and GOOGLE_OAUTH2_CLIENT_SECRET to the application client ID and secret that you created previously.

  7. Save and close

If you are using Cloud SQL:

  1. Open app.yaml for editing.
  2. Set the value of cloud_sql_instances to the same value used for CLOUDSQL_CONNECTION_NAME in . It should be in the format project:region:cloudsql-instance. Uncomment this entire line.
  3. Save and close app.yaml.

Installing dependencies

Enter these commands to create a virtual environment and install dependencies:


virtualenv -p python3 env
source env/bin/activate
pip install -r requirements.txt


virtualenv -p python3 env
pip install -r requirements.txt

Running the app on your local machine:

  1. Start a local web server:

  2. In your web browser, enter this address:


Press Control+C to exit the worker and then the local web server.

Deploying the app to the App Engine flexible environment

  1. Deploy the sample app:

    gcloud app deploy
  2. In your web browser, enter this address. Replace [YOUR_PROJECT_ID] with your project ID:


If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources.

You can reduce costs by deleting the non-default versions of your app.

To delete an app version:

  1. In the GCP Console, go to the App Engine Versions page.

    Go to the Versions page

  2. Click the checkbox next to the non-default app version you want to delete.
  3. Click Delete delete at the top of the page to delete the app version.

For complete information about cleaning up billable resources, see the Cleaning up section in the final step of this tutorial.

Simulate an error

In order to see Stackdriver Error Reporting in action, you can intentionally introduce a mistake into the code so you can see the exception in the Error Reporting console.

In bookshelf/, add an operation that accesses an undefined variable and will throw an ReferenceError in the index view.

def list():

Now redeploy the app:

gcloud app deploy

And access the index page to cause the error:

gcloud app browse

You should see An internal error occurred.

In the Google Cloud Platform Console, select Stackdriver > Error Reporting:

Go to Error Reporting

You should see the error listed in the dashboard:

View Errors

You can then click on the error to see more details about when it was last seen, the number of times it occurred, a histogram of occurrence times, and the stack trace.

Understanding the code

In order to report uncaught exceptions, the code uses the Flask errorhandler decorator, and then uses the Google Cloud Python Client client to report the exception to Stackdriver Error Reporting.

def server_error(e):
    client = error_reporting.Client(app.config['PROJECT_ID'])
    return """
    An internal error occurred.
    """, 500

The client automatically adds the traceback info, and uses a helper function to extract the relevant request details from the Flask request. This way, any uncaught InternalServerError HTTP 500 exception in your application will have the relevant stack traces and HTTP contexts available in Error Reporting.

Note that the 500 error handler only triggers when the app is not in DEBUG mode. When you deploy the app, DEBUG will be off, but by default DEBUG is on locally, so exceptions won't be reported to Error Reporting. Instead, the stack trace information will be rendered directly to the browser. You can change the local debug setting in

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the project:

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Was this page helpful? Let us know how we did:

Send feedback about...