From aa644b43407798fd9dc54ae8f41584bf2b6138eb Mon Sep 17 00:00:00 2001 From: Ilya Sukhanov Date: Tue, 15 Apr 2014 21:15:33 -0400 Subject: [PATCH] Allow returning http errors with exceptions before: def my_response_method(self): ... if error: return template, {'status'=400} after: def my_response_method(self): ... if error: raise MyResponseError("bad thing happened") where MyResponseError inherits from HTTPException --- moto/core/responses.py | 6 +++++- requirements.txt | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index ab1188e0..88e4ea6d 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -3,6 +3,7 @@ import json from urlparse import parse_qs, urlparse +from werkzeug.exceptions import HTTPException from moto.core.utils import camelcase_to_underscores, method_names_from_class @@ -49,7 +50,10 @@ class BaseResponse(object): method_names = method_names_from_class(self.__class__) if action in method_names: method = getattr(self, action) - response = method() + try: + response = method() + except HTTPException as http_error: + response = http_error.description, dict(status=http_error.code) if isinstance(response, basestring): return 200, headers, response else: diff --git a/requirements.txt b/requirements.txt index c6ab212d..4ea7dca3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ requests sure<1.2.4 xmltodict dicttoxml +werkzeug