Skip to content

Commit b37997c

Browse files
thuliorai200890
authored andcommitted
fix: Fix exception check (#7)
* fix: Fix exception check * tests: Add test coverage
1 parent ce38ee9 commit b37997c

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

google_cloud_logger/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def __init__(self, *args, **kwargs):
1919
def _get_extra_fields(self, record):
2020
if hasattr(record, "extra"):
2121
return record.extra
22-
attributes = (field for field in record.__dict__.keys()
23-
if not inspect.ismethod(field))
22+
attributes = (
23+
field for field in record.__dict__.keys() if not inspect.ismethod(field)
24+
)
2425

2526
fields = set(attributes).difference(set(self.reserved_attrs.keys()))
2627
return {key: getattr(record, key) for key in fields if key}
@@ -72,7 +73,7 @@ def make_exception(self, record):
7273
}
7374

7475
def make_metadata(self, record):
75-
if hasattr(record, "exc_info"):
76+
if getattr(record, "exc_info", None):
7677
return {
7778
"userLabels": self.make_user_labels(record),
7879
"exception": self.make_exception(record),

test/google_cloud_logger/test_google_cloud_formatter.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
from collections import OrderedDict
22

33
import pytest
4-
54
from google_cloud_logger import GoogleCloudFormatter
65

6+
# from https://stackoverflow.com/a/19258720
7+
class FakeCode(object):
8+
def __init__(self, co_filename, co_name):
9+
self.co_filename = co_filename
10+
self.co_name = co_name
11+
12+
13+
class FakeFrame(object):
14+
def __init__(self, f_code, f_globals):
15+
self.f_code = f_code
16+
self.f_globals = f_globals
17+
18+
19+
class FakeTraceback(object):
20+
def __init__(self, frames, line_nums):
21+
if len(frames) != len(line_nums):
22+
raise ValueError("Ya messed up!")
23+
self._frames = frames
24+
self._line_nums = line_nums
25+
self.tb_frame = frames[0]
26+
self.tb_lineno = line_nums[0]
27+
28+
@property
29+
def tb_next(self):
30+
if len(self._frames) > 1:
31+
return FakeTraceback(self._frames[1:], self._line_nums[1:])
32+
733

834
@pytest.fixture
935
def formatter():
@@ -45,6 +71,25 @@ def record_with_extra_attribute(log_record_factory, mocker):
4571
return record
4672

4773

74+
@pytest.fixture
75+
def record_with_exception(log_record_factory, mocker):
76+
code = FakeCode("module.py", "function")
77+
frame = FakeFrame(code, {})
78+
traceback = FakeTraceback([frame], [1])
79+
data = {
80+
"asctime": "2018-08-30 20:40:57,245",
81+
"filename": "_internal.py",
82+
"funcName": "_log",
83+
"lineno": "88",
84+
"levelname": "WARNING",
85+
"message": "farofa",
86+
"exc_info": (Exception, "ERROR", traceback),
87+
}
88+
record = log_record_factory(**data)
89+
record.getMessage = mocker.Mock(return_value=data["message"])
90+
return record
91+
92+
4893
def test_add_fields(formatter, record, mocker):
4994
log_record = OrderedDict({})
5095
mocker.patch.object(
@@ -96,14 +141,22 @@ def test_make_metadata(formatter, record):
96141
assert metadata["userLabels"]["extra_field"] == "extra"
97142

98143

99-
def test_make_metadata_with_extra_attribute(
100-
formatter,
101-
record_with_extra_attribute):
144+
def test_make_metadata_with_extra_attribute(formatter, record_with_extra_attribute):
102145
metadata = formatter.make_metadata(record_with_extra_attribute)
103146

104147
assert metadata["userLabels"]["extra_field"] == "extra"
105148

106149

150+
def test_make_metadata_with_exception(formatter, record_with_exception):
151+
metadata = formatter.make_metadata(record_with_exception)
152+
153+
assert metadata["exception"] == {
154+
"class": Exception().__class__,
155+
"message": "ERROR",
156+
"traceback": ' File "module.py", line 1, in function\n',
157+
}
158+
159+
107160
def test_make_source_location(formatter, record):
108161
assert formatter.make_source_location(record) == {
109162
"file": "_internal.py",

0 commit comments

Comments
 (0)