From 5633d7c7612a406a741525ec8d9dc1e1263761c5 Mon Sep 17 00:00:00 2001 From: bmccandless Date: Wed, 29 Jul 2020 13:05:59 -0700 Subject: [PATCH] Fix server exception classes (#1683) str(e) and e.message will both show the error message. refactored the error.py file to simplify our exception class definitions --- server/common/errors.py | 98 +++++++++++++---------------------------- 1 file changed, 30 insertions(+), 68 deletions(-) diff --git a/server/common/errors.py b/server/common/errors.py index d1dda2cf..ca927084 100644 --- a/server/common/errors.py +++ b/server/common/errors.py @@ -1,85 +1,47 @@ from http import HTTPStatus -class RequestException(Exception): +class CellxgeneException(Exception): + """Base class for cellxgene exceptions""" + + def __init__(self, message): + self.message = message + super().__init__(message) + + +class RequestException(CellxgeneException): """Baseclass for exceptions that can be raised from a request.""" # The default status code is 400 (Bad Request) default_status_code = HTTPStatus.BAD_REQUEST def __init__(self, message, status_code=None): - Exception.__init__(self) - self.message = message + super().__init__(message) self.status_code = status_code or self.default_status_code -class FilterError(RequestException): - """Raised when filter is malformed""" - - pass +def define_exception(name, doc): + globals()[name] = type(name, (CellxgeneException,), dict(__doc__=doc)) -class JSONEncodingValueError(RequestException): - """Raised when data cannot be encoded into json""" - - pass +def define_request_exception(name, doc, default_status_code=HTTPStatus.BAD_REQUEST): + globals()[name] = type(name, (RequestException,), dict(__doc__=doc, default_status_code=default_status_code)) -class MimeTypeError(RequestException): - """Raised when incompatible MIME type selected""" +define_request_exception("FilterError", "Raised when filter is malformed") +define_request_exception("JSONEncodingValueError", "Raised when data cannot be encoded into json") +define_request_exception("MimeTypeError", "Raised when incompatible MIME type selected") +define_request_exception("DatasetAccessError", "Raised when file loaded into a DataAdaptor is misformatted") +define_request_exception("DisabledFeatureError", "Raised when an attempt to use a disabled feature occurs") +define_request_exception("AnnotationsError", "Raised when an attempt to use the annotations feature fails") +define_request_exception( + "ComputeError", + "Raised when an error occurs during a compute algorithm (such as diffexp)", + HTTPStatus.INTERNAL_SERVER_ERROR, +) +define_request_exception("ExceedsLimitError", "Raised when an HTTP request exceeds a limit/quota") +define_request_exception("ColorFormatException", "Raised when color helper functions encounter an unknown color format") - pass - - -class DatasetAccessError(RequestException): - """Raised when file loaded into a DataAdaptor is misformatted""" - - pass - - -class DisabledFeatureError(RequestException): - """Raised when an attempt to use a disabled feature occurs""" - - pass - - -class AnnotationsError(RequestException): - """Raised when an attempt to use the annotations feature fails""" - - pass - - -class ComputeError(RequestException): - """Raised when an error occurs during a compute algorithm (such as diffexp)""" - - default_status_code = HTTPStatus.INTERNAL_SERVER_ERROR - - -class ExceedsLimitError(RequestException): - """Raised when an HTTP request exceeds a limit/quota""" - - pass - - -class ColorFormatException(RequestException): - """Raised when color helper functions encounter an unknown color format""" - - pass - - -class OntologyLoadFailure(Exception): - """Raised when reading the ontology file fails""" - - pass - - -class ConfigurationError(Exception): - """Raised when checking configuration errors""" - - pass - - -class PrepareError(Exception): - """Raised when data is misprepared""" - - pass +define_exception("OntologyLoadFailure", "Raised when reading the ontology file fails") +define_exception("ConfigurationError", "Raised when checking configuration errors") +define_exception("PrepareError", "Raised when data is misprepared")