Error Reporting provides a centralized dashboard that shows
counts of each unique error, stack traces, and a history of occurrences. You can
also set up an alert for when 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.
Open the config.py file for editing and replace the
following values:
Set the value of [PROJECT_ID] to your project ID, which
is visible in the Google Cloud console.
Set the value of [DATA_BACKEND] to the same value you used
during the
Using structured data
tutorial.
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.
Set the value of [CLOUD_STORAGE_BUCKET] to your
Cloud Storage bucket name.
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.
Save and close the config.py file.
If you are using Cloud SQL:
Open the app.yaml file for editing.
Set the value of cloudsql-instance to the same value used for
[CLOUDSQL_CONNECTION_NAME] in the config.py
file. Use the format project:region:cloudsql-instance.
Uncomment this entire line.
Save and close the app.yaml file.
Installing dependencies
To create a virtual environment and install dependencies, use the following
commands:
If you update your app, you deploy the updated version by entering the same
command that you used to deploy the app. The deployment creates a new version
of your app and promotes it to the default version. The earlier versions of your
app remain, as do their associated virtual machine (VM) instances. All of these
app versions and VM instances are billable resources. To reduce costs, delete the
non-default versions of your app.
To delete an app version:
In the Google Cloud console, go to the Versions page for App Engine.
Select the checkbox for the non-default app version that you want to
delete.
To delete the app version,
click deleteDelete.
For more information about cleaning up billable resources, see the
Cleaning up
section in the final step of this tutorial.
Simulate an error
To see Error Reporting in action, intentionally introduce a mistake in
your code, and then look for the exception in the Google Cloud console's
Error Reporting page.
In bookshelf/crud.py, add an operation that accesses an undefined variable
and generates a ReferenceError in the index view.
@crud.route("/")
deflist():
x[3]
Deploy the app.
gcloudappdeploy
Access the index page.
gcloudappbrowse
You can view the message An internal error occurred.
In the Google Cloud console, go to the Error Reporting page:
You can also find this page by using the search bar.
You can see the error listed.
Click the error to see information about the error, such as when the error
was last seen,the number of times the error occurred, a histogram of
occurrence times, and the stack trace.
Understanding the code
To report uncaught exceptions, the code first uses the Flask errorhandler
decorator, and then reports the exception to Error Reporting by using
the
Cloud Client Libraries for Python.
@app.errorhandler(500)defserver_error(e):client=error_reporting.Client(app.config['PROJECT_ID'])client.report_exception(http_context=error_reporting.build_flask_context(request))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, which populates
Error Reporting with the relevant stack traces and HTTP contexts for any
uncaught InternalServerErrorHTTP 500 exception in your app.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this
tutorial, either delete the project that contains the resources, or keep the project and
delete the individual resources.
The easiest way to eliminate billing is to delete the project that you
created for the tutorial.
To delete the project:
In the Google Cloud console, go to the Manage resources page.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Reporting errors\n\n*** ** * ** ***\n\nThis tutorial shows how to report and track uncaught exceptions in the\n[Getting started with Python](/python/docs/getting-started)\ntutorial by using the [Google Cloud console](/error-reporting/docs).\n\nError Reporting provides a centralized dashboard that shows\ncounts of each unique error, stack traces, and a history of occurrences. You can\nalso set up an alert for when errors occur.\n\nThis page is part of a multipage tutorial. To start from the beginning and read\nthe setup instructions, go to\n[Python Bookshelf app](/python/monitor-and-debug/tutorial-app). \n\nConfiguring settings\n--------------------\n\nThis section uses code in the `5-logging` directory. Edit the files\nand run commands in this directory.\n\n1. Open the `config.py` file for editing and replace the following values:\n - Set the value of `[PROJECT_ID]` to your project ID, which is visible in the Google Cloud console.\n - Set the value of `[DATA_BACKEND]` to the same value you used during the [Using structured data](/python/getting-started/using-structured-data) tutorial.\n - 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.\n - Set the value of `[CLOUD_STORAGE_BUCKET]` to your Cloud Storage bucket name.\n - Under the `OAuth2 configuration` section, set the values\n of `[GOOGLE_OAUTH2_CLIENT_ID]` and\n `[GOOGLE_OAUTH2_CLIENT_SECRET]` to the application client\n ID and secret that you created previously.\n\n2. Save and close the `config.py` file.\n\nIf you are using Cloud SQL:\n\n1. Open the `app.yaml` file for editing.\n2. Set the value of `cloudsql-instance` to the same value used for `[CLOUDSQL_CONNECTION_NAME]` in the `config.py` file. Use the format `project:region:cloudsql-instance`. Uncomment this entire line.\n3. Save and close the `app.yaml` file.\n\nInstalling dependencies\n-----------------------\n\nTo create a virtual environment and install dependencies, use the following\ncommands: \n\n### Linux/macOS\n\n virtualenv -p python3 env\n source env/bin/activate\n pip install -r requirements.txt\n\n### Windows\n\n virtualenv -p python3 env\n env\\scripts\\activate\n pip install -r requirements.txt\n\nRunning the app on your local machine\n-------------------------------------\n\n1. Start a local web server:\n\n python main.py\n\n2. In your browser, enter the following address:\n\n http://localhost:8080\n\nPress \u003ckbd\u003eControl+C\u003c/kbd\u003e to exit the worker and then the local web server.\n\nDeploying the app to the App Engine flexible environment\n--------------------------------------------------------\n\n1. Deploy the sample app:\n\n gcloud app deploy\n\n2. In your browser, enter the following URL:\n\n `https://`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e`.r.appspot.com`\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: Your Google Cloud project ID\n - \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e: A [code that App Engine assigns\n to your app](/appengine/docs/flexible/python/how-requests-are-routed#region-id)\n\nIf you update your app, you deploy the updated version by entering the same\ncommand that you used to deploy the app. The deployment creates a new version\nof your app and promotes it to the default version. The earlier versions of your\napp remain, as do their associated virtual machine (VM) instances. All of these\napp versions and VM instances are billable resources. To reduce costs, delete the\nnon-default versions of your app.\n\nTo delete an app version:\n\n1. In the Google Cloud console, go to the **Versions** page for App Engine.\n\n [Go to Versions](https://console.cloud.google.com/appengine/versions)\n2. Select the checkbox for the non-default app version that you want to delete. **Note:** The only way you can delete the default version of your App Engine app is by deleting your project. However, you can [stop the default version in the\n | Google Cloud console](https://console.cloud.google.com/appengine/versions). This action shuts down all instances associated with the version. You can restart these instances later if needed.\n |\n | \u003cbr /\u003e\n |\n | In the App Engine standard environment, you can stop the default version only if your app has\n | manual or basic scaling.\n |\n3. To delete the app version, click delete **Delete**.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nFor more information about cleaning up billable resources, see the\n[Cleaning up](/python/getting-started/using-pub-sub#clean-up)\nsection in the final step of this tutorial.\n\nSimulate an error\n-----------------\n\nTo see Error Reporting in action, intentionally introduce a mistake in\nyour code, and then look for the exception in the Google Cloud console's\nError Reporting page.\n\n1. In `bookshelf/crud.py`, add an operation that accesses an undefined variable\n and generates a `ReferenceError` in the index view.\n\n ```bash\n @crud.route(\"/\")\n def list():\n x[3]\n ```\n2. Deploy the app.\n\n ```bash\n gcloud app deploy\n ```\n3. Access the index page.\n\n ```bash\n gcloud app browse\n ```\n\n You can view the message `An internal error occurred`.\n4. In the Google Cloud console, go to the **Error Reporting** page:\n\n [Go to **Error Reporting**](https://console.cloud.google.com/errors)\n\n \u003cbr /\u003e\n\n You can also find this page by using the search bar.\n\n You can see the error listed.\n\n5. Click the error to see information about the error, such as when the error\n was last seen,the number of times the error occurred, a histogram of\n occurrence times, and the stack trace.\n\nUnderstanding the code\n----------------------\n\nTo report uncaught exceptions, the code first uses the Flask `errorhandler`\ndecorator, and then reports the exception to Error Reporting by using\nthe\n[Cloud Client Libraries for Python](https://github.com/GoogleCloudPlatform/google-cloud-python). \n\n @app.errorhandler(500)\n def server_error(e):\n client = error_reporting.Client(app.config['PROJECT_ID'])\n client.report_exception(\n http_context=error_reporting.build_flask_context(request))\n return \"\"\"\n An internal error occurred.\n \"\"\", 500\n\nThe client automatically adds the traceback info and uses a helper function to\nextract the relevant request details from the Flask request, which populates\nError Reporting with the relevant stack traces and HTTP contexts for any\nuncaught `InternalServerError` `HTTP 500` exception in your app.\n| **Note:** The `500` error handler only triggers when the app isn't in `DEBUG` mode. `DEBUG` is on locally by default, so stack trace information is rendered in the browser. You can change the local debug setting in `main.py`.\n\nClean up\n--------\n\n\nTo avoid incurring charges to your Google Cloud account for the resources used in this\ntutorial, either delete the project that contains the resources, or keep the project and\ndelete the individual resources.\n\n\nThe easiest way to eliminate billing is to delete the project that you\ncreated for the tutorial.\n\nTo delete the project:\n\n| **Caution** : Deleting a project has the following effects:\n|\n| - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.\n| - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.\n|\n|\n| If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects\n| can help you avoid exceeding project quota limits.\n1. In the Google Cloud console, go to the **Manage resources** page.\n\n [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)\n2. In the project list, select the project that you want to delete, and then click **Delete**.\n3. In the dialog, type the project ID, and then click **Shut down** to delete the project.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n[\\\u003c Prev](/python/monitor-and-debug/uptime-alert) Next \\\u003e"]]