Add support for setting HTTP status codes in JsonableError.

This commit is contained in:
Tim Abbott 2016-04-21 12:47:01 -07:00
parent b38c50c6bb
commit 3cde06ea33
4 changed files with 17 additions and 7 deletions

View File

@ -393,8 +393,9 @@ def internal_notify_view(view_func):
return _wrapped_view_func return _wrapped_view_func
class JsonableError(Exception): class JsonableError(Exception):
def __init__(self, error): def __init__(self, error, status_code=400):
self.error = error self.error = error
self.status_code = status_code
def __str__(self): def __str__(self):
return self.to_json_error_msg() return self.to_json_error_msg()
@ -403,16 +404,18 @@ class JsonableError(Exception):
return self.error return self.error
class RequestVariableMissingError(JsonableError): class RequestVariableMissingError(JsonableError):
def __init__(self, var_name): def __init__(self, var_name, status_code=400):
self.var_name = var_name self.var_name = var_name
self.status_code = status_code
def to_json_error_msg(self): def to_json_error_msg(self):
return "Missing '%s' argument" % (self.var_name,) return "Missing '%s' argument" % (self.var_name,)
class RequestVariableConversionError(JsonableError): class RequestVariableConversionError(JsonableError):
def __init__(self, var_name, bad_value): def __init__(self, var_name, bad_value, status_code=400):
self.var_name = var_name self.var_name = var_name
self.bad_value = bad_value self.bad_value = bad_value
self.status_code = status_code
def to_json_error_msg(self): def to_json_error_msg(self):
return "Bad value for '%s': %s" % (self.var_name, self.bad_value) return "Bad value for '%s': %s" % (self.var_name, self.bad_value)

View File

@ -256,7 +256,12 @@ class LogRequests(object):
class JsonErrorHandler(object): class JsonErrorHandler(object):
def process_exception(self, request, exception): def process_exception(self, request, exception):
if hasattr(exception, 'to_json_error_msg') and callable(exception.to_json_error_msg): if hasattr(exception, 'to_json_error_msg') and callable(exception.to_json_error_msg):
return json_error(exception.to_json_error_msg()) try:
status_code = exception.status_code
except Exception:
logging.warning("Jsonable exception %s missing status code!" % (exception,))
status_code = 400
return json_error(exception.to_json_error_msg(), status=status_code)
if request.error_format == "JSON": if request.error_format == "JSON":
logging.error(traceback.format_exc()) logging.error(traceback.format_exc())
return json_error("Internal server error", status=500) return json_error("Internal server error", status=500)

View File

@ -75,9 +75,10 @@ def get_sqlalchemy_connection():
sa_connection.execution_options(autocommit=False) sa_connection.execution_options(autocommit=False)
return sa_connection return sa_connection
class BadNarrowOperator(Exception): class BadNarrowOperator(JsonableError):
def __init__(self, desc): def __init__(self, desc, status_code=400):
self.desc = desc self.desc = desc
self.status_code = status_code
def to_json_error_msg(self): def to_json_error_msg(self):
return 'Invalid narrow operator: ' + self.desc return 'Invalid narrow operator: ' + self.desc

View File

@ -81,8 +81,9 @@ def list_to_streams(streams_raw, user_profile, autocreate=False, invite_only=Fal
return existing_streams, created_streams return existing_streams, created_streams
class PrincipalError(JsonableError): class PrincipalError(JsonableError):
def __init__(self, principal): def __init__(self, principal, status_code=400):
self.principal = principal self.principal = principal
self.status_code = status_code
def to_json_error_msg(self): def to_json_error_msg(self):
return ("User not authorized to execute queries on behalf of '%s'" return ("User not authorized to execute queries on behalf of '%s'"