Skip to content

Commit

Permalink
catch unexpected exceptions in handler
Browse files Browse the repository at this point in the history
The wsgi.ResourceExceptionHandler should catch also the unexpected
exceptions and render a 500 error to the client.
  • Loading branch information
alvarolopez committed Apr 21, 2015
1 parent 81b8eac commit a55edb3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
66 changes: 66 additions & 0 deletions ooi/tests/test_resource_exception_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-

# Copyright 2015 Spanish National Research Council
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import webob
import webob.dec
import webob.exc

from ooi import exception
from ooi.tests import base
from ooi import wsgi


class TestResourceExceptionHandler(base.TestCase):
def test_invalid(self):
ret = self.return_fault_with_handler(exception.Invalid)
self.assertIsInstance(ret, wsgi.Fault)
self.assertIsInstance(ret.wrapped_exc, webob.exc.WSGIHTTPException)
self.assertEqual(400, ret.wrapped_exc.code)

def test_not_implemented(self):
ret = self.return_fault_with_handler(exception.NotImplemented)
self.assertIsInstance(ret, wsgi.Fault)
self.assertEqual(webob.exc.HTTPNotImplemented.code,
ret.wrapped_exc.code)

def test_bad_request(self):
ret = self.return_fault_with_handler(TypeError)
self.assertIsInstance(ret, wsgi.Fault)
self.assertIsInstance(ret.wrapped_exc, webob.exc.HTTPBadRequest)

def test_fault(self):
fault = wsgi.Fault(webob.exc.HTTPInternalServerError())
ret = self.return_fault_with_handler(fault)
self.assertIs(ret, fault)

def test_http_exception(self):
ret = self.return_fault_with_handler(webob.exc.HTTPNotFound())
self.assertIsInstance(ret, wsgi.Fault)
self.assertIsInstance(ret.wrapped_exc, webob.exc.HTTPNotFound)

def test_internal_server_error(self):
ret = self.return_fault_with_handler(Exception)
self.assertIsInstance(ret, wsgi.Fault)
self.assertIsInstance(ret.wrapped_exc,
webob.exc.HTTPInternalServerError)

@staticmethod
def return_fault_with_handler(ex):
try:
with wsgi.ResourceExceptionHandler():
raise ex
except wsgi.Fault as e:
return e
3 changes: 3 additions & 0 deletions ooi/wsgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ def __exit__(self, ex_type, ex_value, ex_traceback):
elif isinstance(ex_value, webob.exc.HTTPException):
LOG.info("HTTP exception thrown: %s", ex_value)
raise Fault(ex_value)
else:
LOG.warning("Unexpected exception: %s" % ex_value)
raise Fault(webob.exc.HTTPInternalServerError())

# We didn't handle the exception
return False
Expand Down

0 comments on commit a55edb3

Please sign in to comment.