Ensure that 404's are returned as JSON

Overide default Pecan 404 behaviour, and return a error from middleware
like all other errors

Change-Id: Idd2a051f5ce298f7cc5327ceb2946cfc30fe68ac
Closes-Bug: #1333347
(cherry picked from commit 047f94c178)
This commit is contained in:
Graham Hayes 2014-06-23 17:57:59 +01:00 committed by Ryan Petrello
parent fca665396f
commit 7dfb026d78
4 changed files with 18 additions and 3 deletions

View File

@ -223,8 +223,9 @@ class FaultWrapperMiddleware(wsgi.Middleware):
return self._handle_exception(request, e)
def _handle_exception(self, request, e, status=500, response={}):
# Log the exception ASAP
LOG.exception(e)
# Log the exception ASAP unless it is a 404 Not Found
if not getattr(e, 'expected', False):
LOG.exception(e)
headers = [
('Content-Type', 'application/json'),

View File

@ -33,7 +33,11 @@ def factory(global_config, **local_conf):
conf = {
'app': {
'root': 'designate.api.v2.controllers.root.RootController',
'modules': ['designate.api.v2']
'modules': ['designate.api.v2'],
'errors': {
404: '/not_found',
'__force_dict__' : True
}
}
}

View File

@ -13,6 +13,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from designate import exceptions
from designate.openstack.common import log as logging
from designate.api.v2.controllers import limits
from designate.api.v2.controllers import reverse
@ -21,6 +22,8 @@ from designate.api.v2.controllers import tlds
from designate.api.v2.controllers import zones
from designate.api.v2.controllers import blacklists
from pecan import expose
LOG = logging.getLogger(__name__)
@ -35,3 +38,9 @@ class RootController(object):
tlds = tlds.TldsController()
zones = zones.ZonesController()
blacklists = blacklists.BlacklistsController()
@expose(content_type='text/plain')
@expose(content_type='text/dns')
@expose(content_type='application/json')
def not_found(self):
raise exceptions.NotFound

View File

@ -197,6 +197,7 @@ class DuplicateBlacklist(Duplicate):
class NotFound(Base):
expected = True
error_code = 404
error_type = 'not_found'