Skip to content

Commit

Permalink
fix: logg serverfeil gjennom rot-loggeren - og test det
Browse files Browse the repository at this point in the history
  • Loading branch information
stigok committed Jan 28, 2020
1 parent 8089a16 commit 50bd067
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ruterstop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def not_found_error_handler(res):

def default_error_handler(res):
res.set_header("Content-Type", "text/plain")
print(res.traceback, file=sys.stderr)
log.error(res.traceback)
return "Feil på serveren"

webapp.default_error_handler = default_error_handler
Expand Down
16 changes: 10 additions & 6 deletions ruterstop/tests/test_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ def test_simple_404_error(self, mock):

@patch("ruterstop.get_departures", return_value=None)
def test_simple_500_error(self, mock):
mock.side_effect = Exception("barf")
res = self.app.get("/1234", expect_errors=True)
self.assertEqual(res.content_type, "text/plain")
self.assertEqual(res.status_code, 500)
self.assertEqual(res.body, "Feil på serveren".encode())
self.assertEqual(mock.call_count, 1)
mock.side_effect = Exception("barf voof")

with self.assertLogs(logger="ruterstop", level="ERROR") as log:
res = self.app.get("/1234", expect_errors=True)
self.assertEqual(res.content_type, "text/plain")
self.assertEqual(res.status_code, 500)
self.assertEqual(res.body, "Feil på serveren".encode())
self.assertEqual(mock.call_count, 1)

self.assertRegex(log.output[0], r"barf voof")

@patch("ruterstop.format_departure_list", return_value=None)
@patch("ruterstop.get_departures", return_value=dict(a="foo"))
Expand Down

0 comments on commit 50bd067

Please sign in to comment.