# Using Error Reporting

1.16.0 (latest) 1.15.0 1.14.0 1.13.0 1.12.0 1.11.1 1.10.0 1.9.2 1.8.2 1.7.0 1.6.3 1.5.3 1.4.1 1.3.0 1.2.3 1.1.2 1.0.0 0.34.0 0.33.0 0.32.1 After configuring your environment, create a
[`Client`](https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/client#google.cloud.error_reporting.client.Client)

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html()

or pass in `credentials` and `project` explicitly

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html(project='my-project', credentials=creds)

Error Reporting associates errors with a service, which is an identifier for
an executable, App Engine service, or job. The default service is "python",
but a default can be specified for the client on construction time. You can
also optionally specify a version for that service, which defaults to
"default."

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html(
     project='my-project', service="login_service", version="0.1.0")

## Reporting an exception

Report a stacktrace to Error Reporting after an exception:

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html()
    try:
        raise NameError
    except Exception:
        https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.html.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html#google_cloud_error_reporting_client_Client_report_exception()

By default, the client will report the error using the service specified in
the client's constructor, or the default service of "python".

The user and HTTP context can also be included in the exception. The HTTP
context can be constructed using
`google.cloud.error_reporting.HTTPContext`. This will be used by
Error Reporting to help group exceptions.

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html()
    user = 'example@gmail.com'
    http_context = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.HTTPContext.html(
        method='GET', url='/', user_agent='test agent',
        referrer='example.com', response_status_code=500,
        remote_ip='1.2.3.4')
    try:
        raise NameError
    except Exception:
        client.report_exception(http_context=http_context, user=user))

An automatic helper to build the HTTP Context from a Flask (Werkzeug) request
object is provided.

    from google.cloud.error_reporting import build_flask_context

    @app.errorhandler(HTTPException)
    def handle_error(exc):
        client.report_exception(
            http_context=build_flask_context(request))
        # rest of error response code here

## Reporting an error without an exception

Errors can also be reported to Error Reporting outside the context
of an exception. The library will include the file path, function name, and
line number of the location where the error was reported.

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html()
    https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.html.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html("Found an error!")

As with reporting an exception, the user and HTTP context can be provided:

    from google.cloud import error_reporting

    client = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.Client.html()
    user = 'example@gmail.com'
    http_context = error_reporting.https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/google.cloud.error_reporting.client.HTTPContext.html(
        method='GET', url='/', user_agent='test agent',
        referrer='example.com', response_status_code=500,
        remote_ip='1.2.3.4')
    client.report(
        "Found an error!", http_context=http_context, user=user))