Skip to content

Commit 51e1b47

Browse files
author
Bill Prin
committed
Add Flask Helper to Error Reporting
1 parent 67a5539 commit 51e1b47

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

packages/google-cloud-error-reporting/google/cloud/error_reporting/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717

1818
from google.cloud.error_reporting.client import Client
19+
from google.cloud.error_reporting.client import HTTPContext
20+
from google.cloud.error_reporting.util import build_flask_context
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2016 Google Inc. 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.
14+
15+
"""Utility functions for Stackdriver Error Reporting."""
16+
17+
from google.cloud.error_reporting.client import HTTPContext
18+
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+
.. code-block:: python
27+
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
33+
34+
:type request: :class:`werkzeug.wrappers.request`
35+
:param request: The Flask request object to convert.
36+
37+
:rtype: :class:`~google.cloud.error_reporting.client.HTTPContext`
38+
:returns: An HTTPContext object ready to be sent to the Stackdriver Error
39+
Reporting API.
40+
"""
41+
return HTTPContext(url=request.url, method=request.method,
42+
user_agent=request.user_agent.string,
43+
referrer=request.referrer,
44+
remote_ip=request.remote_addr)

packages/google-cloud-error-reporting/tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ localdeps =
1010
deps =
1111
{toxinidir}/../core
1212
{toxinidir}/../logging
13+
mock
1314
pytest
1415
covercmd =
1516
py.test --quiet \
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2016 Google Inc. 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.
14+
15+
import unittest
16+
17+
18+
class Test_build_flask_context(unittest.TestCase):
19+
20+
def _call_fut(self, request):
21+
from google.cloud.error_reporting.util import build_flask_context
22+
return build_flask_context(request)
23+
24+
def test_flask_helper(self):
25+
import mock
26+
27+
user_agent = mock.Mock(string='Google Cloud Unit Tests Agent')
28+
request = _Request('http://google.com', 'GET',
29+
user_agent,
30+
'http://gmail.com',
31+
'127.0.0.1')
32+
33+
context = self._call_fut(request)
34+
self.assertEqual(request.url, context.url)
35+
self.assertEqual(request.method, context.method)
36+
self.assertEqual(request.user_agent.string, context.userAgent)
37+
self.assertEqual(request.referrer, context.referrer)
38+
self.assertEqual(request.remote_addr, context.remoteIp)
39+
40+
41+
class _Request(object):
42+
43+
def __init__(self, url, method, user_agent, referrer, remote_addr):
44+
self.url = url
45+
self.method = method
46+
self.user_agent = user_agent
47+
self.referrer = referrer
48+
self.remote_addr = remote_addr

0 commit comments

Comments
 (0)