Skip to content

Commit 3006a73

Browse files
author
Bill Prin
committed
Add Flask Helper to Error Reporting
1 parent b92f0cf commit 3006a73

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

docs/error-reporting-usage.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ be used by Stackdriver Error Reporting to help group exceptions.
7676
>>> except Exception:
7777
>>> client.report_exception(http_context=http_context, user=user))
7878
79+
An automatic helper to build the HTTP Context from a Flask (Werkzeug) request
80+
object is provided.
81+
82+
.. code-block:: python
83+
84+
>>> @app.errorhandler(Exception)
85+
>>> def handle_error(e):
86+
>>> client = error_reporting.Client()
87+
>>> client.report_exception(
88+
>>> http_context=build_flask_context(request))
89+
>>> # rest of error response code here
90+
91+
7992
Reporting an error without an exception
8093
-----------------------------------------
8194

error_reporting/google/cloud/error_reporting/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,32 @@
1414

1515
"""Client library for Stackdriver Error Reporting"""
1616

17+
from google.cloud.error_reporting.client import Client, HTTPContext
1718

18-
from google.cloud.error_reporting.client import Client
19+
20+
def build_flask_context(request):
21+
"""Builds an HTTP context object from a Flask (Werkzeug) request object.
22+
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.
25+
26+
Example::
27+
28+
>>> @app.errorhandler(Exception)
29+
>>> def handle_error(e):
30+
>>> client = error_reporting.Client()
31+
>>> client.report_exception(
32+
>>> http_context=build_flask_context(request))
33+
>>> # rest of error response code here
34+
35+
:type request: :class:`werkzeug.wrappers.request`
36+
:param request: The Flask request object to convert.
37+
38+
:rtype: :class:`google.cloud.error_reporting.client.HTTPContext`
39+
:returns: An HTTPContext object ready to be sent to the Stackdriver Error
40+
Reporting API.
41+
"""
42+
return HTTPContext(url=request.url, method=request.method,
43+
user_agent=str(request.user_agent),
44+
referrer=request.referrer,
45+
remote_ip=request.remote_addr)

error_reporting/unit_tests/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,34 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
16+
import unittest
17+
18+
19+
class TestFlaskHelper(unittest.TestCase):
20+
21+
def test_flask_helper(self):
22+
from google.cloud.error_reporting import build_flask_context
23+
request = _Request('http://google.com', 'GET',
24+
'Google Cloud Unit Tests Agent', 'http://gmail.com',
25+
'127.0.0.1')
26+
context = build_flask_context(request)
27+
self.assertEqual(request.url, context.url)
28+
self.assertEqual(request.method, context.method)
29+
self.assertEqual(request.user_agent, context.user_agent)
30+
self.assertEqual(request.referrer, context.referrer)
31+
self.assertEqual(request.remote_ip, context.remote_ip)
32+
33+
34+
# Fake request so no need to bring in Werkzeug as dependency
35+
class _Request(object):
36+
37+
def __init__(self, url, method, user_agent, referrer, remote_ip):
38+
self.url = url
39+
self.method = method
40+
self.user_agent = user_agent
41+
self.referrer = referrer
42+
self.remote_ip = remote_ip
43+
44+

0 commit comments

Comments
 (0)