Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Refactor handle unexpected exception
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Dec 31, 2016
1 parent 136a525 commit 200befa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
21 changes: 15 additions & 6 deletions kobin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ def _handle(self, environ):
except HTTPError as e:
response = e
except BaseException as e:
traceback.print_tb(e.__traceback__)
if self.config['DEBUG']:
message = '\n'.join(traceback.format_tb(e.__traceback__))
else:
message = 'Internal Server Error'
response = HTTPError(message, 500)
response = _handle_unexpected_exception(e, self.config.get('DEBUG'))
return response

def wsgi(self, environ, start_response):
Expand All @@ -90,6 +85,20 @@ def __call__(self, environ, start_response):
return self.wsgi(environ, start_response)


def _get_traceback_message(e):
message = '\n'.join(traceback.format_tb(e.__traceback__))
return message


def _handle_unexpected_exception(e, debug):
if debug:
message = _get_traceback_message(e)
else:
message = 'Internal Server Error'
response = HTTPError(message, 500)
return response


class Config(dict):
"""This class manages your application configs.
"""
Expand Down
4 changes: 3 additions & 1 deletion kobin/app.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from types import ModuleType
from jinja2 import Environment # type: ignore

from .routes import Router
from .environs import BaseResponse
from .environs import BaseResponse, Response


WSGIEnvironValue = TypeVar('WSGIEnvironValue')
Expand All @@ -30,6 +30,8 @@ class Kobin:
def wsgi(self, environ: WSGIEnviron, start_response: StartResponse) -> WSGIResponse: ...
def __call__(self, environ: WSGIEnviron, start_response: StartResponse) -> WSGIResponse: ...

def _get_traceback_message(e: BaseException) -> str: ...
def _handle_unexpected_exception(e: BaseException, debug: bool) -> Response: ...

class Config(dict):
_template_env: Environment
Expand Down
38 changes: 37 additions & 1 deletion tests/test_apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import os
from unittest import TestCase
from kobin import Kobin, Config, Response, load_config_from_module, load_config_from_pyfile
from kobin import (
Kobin, Response,
Config, load_config_from_module, load_config_from_pyfile
)
from kobin.app import _handle_unexpected_exception, _get_traceback_message


class KobinTests(TestCase):
Expand Down Expand Up @@ -117,6 +121,38 @@ def test_after_request(self):
self.assertIn(('Foo', 'Bar'), response.headerlist)


class HandleUnexpectedExceptionTests(TestCase):
def test_get_traceback_message(self):
try:
1 / 0
except BaseException as e:
actual = _get_traceback_message(e)
else:
actual = "Exception is not raised."
cause_of_exception = 'x = 1/0'
self.assertIn(cause_of_exception, actual)

def test_handle_unexpected_exception_should_return_500(self):
try:
1 / 0
except BaseException as e:
actual = _handle_unexpected_exception(e, False)
else:
actual = "Exception is not raised."
expected_status_code = 500
self.assertEqual(actual.status_code, expected_status_code)

def test_handle_unexpected_exception_body(self):
try:
1 / 0
except BaseException as e:
actual = _handle_unexpected_exception(e, False)
else:
actual = "Exception is not raised."
expected_body = [b'Internal Server Error']
self.assertEqual(actual.body, expected_body)


class KobinAfterHookTests(TestCase):
def setUp(self):
self.app = Kobin()
Expand Down

0 comments on commit 200befa

Please sign in to comment.