Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/error_reporting/util.py: 100%
4 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:45 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:45 +0000
1# Copyright 2016 Google LLC All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""Utility functions for Error Reporting."""
17from google.cloud.error_reporting.client import HTTPContext
20def build_flask_context(request):
21 """Builds an HTTP context object from a Flask (Werkzeug) request object.
23 This helper method extracts the relevant HTTP context from a Flask request
24 object into an object ready to be sent to Error Reporting.
26 .. code-block:: python
28 >>> @app.errorhandler(HTTPException)
29 ... def handle_error(exc):
30 ... client.report_exception(
31 ... http_context=build_flask_context(request))
32 ... # rest of error response code here
34 :type request: :class:`werkzeug.wrappers.request`
35 :param request: The Flask request object to convert.
37 :rtype: :class:`~google.cloud.error_reporting.client.HTTPContext`
38 :returns: An HTTPContext object ready to be sent to the Error Reporting
39 API.
40 """
41 return HTTPContext(
42 url=request.url,
43 method=request.method,
44 user_agent=request.user_agent.string,
45 referrer=request.referrer,
46 remote_ip=request.remote_addr,
47 )