mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 14:03:30 +00:00 
			
		
		
		
	Add support for setting HTTP status codes in JsonableError.
This commit is contained in:
		@@ -393,8 +393,9 @@ def internal_notify_view(view_func):
 | 
			
		||||
    return _wrapped_view_func
 | 
			
		||||
 | 
			
		||||
class JsonableError(Exception):
 | 
			
		||||
    def __init__(self, error):
 | 
			
		||||
    def __init__(self, error, status_code=400):
 | 
			
		||||
        self.error = error
 | 
			
		||||
        self.status_code = status_code
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return self.to_json_error_msg()
 | 
			
		||||
@@ -403,16 +404,18 @@ class JsonableError(Exception):
 | 
			
		||||
        return self.error
 | 
			
		||||
 | 
			
		||||
class RequestVariableMissingError(JsonableError):
 | 
			
		||||
    def __init__(self, var_name):
 | 
			
		||||
    def __init__(self, var_name, status_code=400):
 | 
			
		||||
        self.var_name = var_name
 | 
			
		||||
        self.status_code = status_code
 | 
			
		||||
 | 
			
		||||
    def to_json_error_msg(self):
 | 
			
		||||
        return "Missing '%s' argument" % (self.var_name,)
 | 
			
		||||
 | 
			
		||||
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.bad_value = bad_value
 | 
			
		||||
        self.status_code = status_code
 | 
			
		||||
 | 
			
		||||
    def to_json_error_msg(self):
 | 
			
		||||
        return "Bad value for '%s': %s" % (self.var_name, self.bad_value)
 | 
			
		||||
 
 | 
			
		||||
@@ -256,7 +256,12 @@ class LogRequests(object):
 | 
			
		||||
class JsonErrorHandler(object):
 | 
			
		||||
    def process_exception(self, request, exception):
 | 
			
		||||
        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":
 | 
			
		||||
            logging.error(traceback.format_exc())
 | 
			
		||||
            return json_error("Internal server error", status=500)
 | 
			
		||||
 
 | 
			
		||||
@@ -75,9 +75,10 @@ def get_sqlalchemy_connection():
 | 
			
		||||
    sa_connection.execution_options(autocommit=False)
 | 
			
		||||
    return sa_connection
 | 
			
		||||
 | 
			
		||||
class BadNarrowOperator(Exception):
 | 
			
		||||
    def __init__(self, desc):
 | 
			
		||||
class BadNarrowOperator(JsonableError):
 | 
			
		||||
    def __init__(self, desc, status_code=400):
 | 
			
		||||
        self.desc = desc
 | 
			
		||||
        self.status_code = status_code
 | 
			
		||||
 | 
			
		||||
    def to_json_error_msg(self):
 | 
			
		||||
        return 'Invalid narrow operator: ' + self.desc
 | 
			
		||||
 
 | 
			
		||||
@@ -81,8 +81,9 @@ def list_to_streams(streams_raw, user_profile, autocreate=False, invite_only=Fal
 | 
			
		||||
    return existing_streams, created_streams
 | 
			
		||||
 | 
			
		||||
class PrincipalError(JsonableError):
 | 
			
		||||
    def __init__(self, principal):
 | 
			
		||||
    def __init__(self, principal, status_code=400):
 | 
			
		||||
        self.principal = principal
 | 
			
		||||
        self.status_code = status_code
 | 
			
		||||
 | 
			
		||||
    def to_json_error_msg(self):
 | 
			
		||||
        return ("User not authorized to execute queries on behalf of '%s'"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user