def test_unhandled_error(self):
     handler = ErrorHandler(self.app, debug=False)
     with self.app.test_request_context('/'):
         response = None
         try:
             self.raise_exception()
         except Exception, exception:
             response = handler.handle(exception)
         eq_(500, response.status_code)
         eq_("An internal error occured", response.data)
class TestErrorHandler(object):
    def setup(self):
        self.app = Flask(__name__)
        Babel(self.app)

    def raise_exception(self, cls=Exception):
        """Simulate an exception that happens deep within the stack."""
        raise cls()

    def test_unhandled_error(self):
        handler = ErrorHandler(self.app, debug=False)
        with self.app.test_request_context('/'):
            response = None
            try:
                self.raise_exception()
            except Exception, exception:
                response = handler.handle(exception)
            eq_(500, response.status_code)
            eq_("An internal error occured", response.data)

        # Try it again with debug=True to get a stack trace instead of
        # a generic error message.
        handler = ErrorHandler(self.app, debug=True)
        with self.app.test_request_context('/'):
            response = None
            try:
                self.raise_exception()
            except Exception, exception:
                response = handler.handle(exception)
            eq_(500, response.status_code)
            assert response.data.startswith(
                'Traceback (most recent call last)')
    def test_handle_error_as_problem_detail_document(self):
        class CanBeProblemDetailDocument(Exception):
            def as_problem_detail_document(self, debug):
                return INVALID_URN.detailed(
                    _("detail info"),
                    debug_message=
                    "A debug_message which should only appear in debug mode.")

        handler = ErrorHandler(self.app, debug=False)
        with self.app.test_request_context('/'):
            try:
                self.raise_exception(CanBeProblemDetailDocument)
            except Exception, exception:
                response = handler.handle(exception)

            eq_(400, response.status_code)
            data = json.loads(response.data)
            eq_(INVALID_URN.title, data['title'])

            # Since we are not in debug mode, the debug_message is
            # destroyed.
            assert 'debug_message' not in data
        handler = ErrorHandler(self.app, debug=False)
        with self.app.test_request_context('/'):
            try:
                self.raise_exception(CanBeProblemDetailDocument)
            except Exception, exception:
                response = handler.handle(exception)

            eq_(400, response.status_code)
            data = json.loads(response.data)
            eq_(INVALID_URN.title, data['title'])

            # Since we are not in debug mode, the debug_message is
            # destroyed.
            assert 'debug_message' not in data

        # Now try it with debug=True and see that the debug_message is
        # preserved and a stack trace is append it to it.
        handler = ErrorHandler(self.app, debug=True)
        with self.app.test_request_context('/'):
            try:
                self.raise_exception(CanBeProblemDetailDocument)
            except Exception, exception:
                response = handler.handle(exception)

            eq_(400, response.status_code)
            data = json.loads(response.data)
            eq_(INVALID_URN.title, data['title'])
            assert data['debug_message'].startswith(
                u"A debug_message which should only appear in debug mode.\n\n"
                u'Traceback (most recent call last)')