Custom Python Exceptions with Error Codes and Error Messages

此生再无相见时 提交于 2019-12-18 10:17:01

问题


class AppError(Exception):
    pass

class MissingInputError(AppError):
    pass

class ValidationError(AppError):
    pass

...

def validate(self):
    """ Validate Input and save it """

    params = self.__params

    if 'key' in params:
        self.__validateKey(escape(params['key'][0]))
    else:
        raise MissingInputError

    if 'svc' in params:
        self.__validateService(escape(params['svc'][0]))
    else:
        raise MissingInputError

    if 'dt' in params:
        self.__validateDate(escape(params['dt'][0]))
    else:
        raise MissingInputError


def __validateMulti(self, m):
    """ Validate Multiple Days Request"""

    if m not in Input.__validDays:
        raise ValidationError

    self.__dCast = int(m)

validate() and __validateMulti() are methods of a class that validates and store the passed input parameters. As is evident in the code, I raise some custom exceptions when some input parameter is missing or some validation fails.

I'd like to define some custom error codes and error messages specific to my app like,

Error 1100: "Key parameter not found. Please verify your input."

Error 1101: "Date parameter not found. Please verify your input"

...

Error 2100: "Multiple Day parameter is not valid. Accepted values are 2, 5 and 7."

and report the same to the user.

  1. How do I define these error codes and error messages in the custom exceptions?
  2. How do I raise / trap exception in a way that I know what error code / message to display?

(P.S: This is for Python 2.4.3).


Bastien Léonard mentions in this SO comment that you don't need to always define a new __init__ or __str__; by default, arguments will be placed in self.args and they will be printed by __str__.

Thus, the solution I prefer:

class AppError(Exception): pass

class MissingInputError(AppError):

    # define the error codes & messages here
    em = {1101: "Some error here. Please verify.", \
          1102: "Another here. Please verify.", \
          1103: "One more here. Please verify.", \
          1104: "That was idiotic. Please verify."}

Usage:

try:
    # do something here that calls
    # raise MissingInputError(1101)

except MissingInputError, e
    print "%d: %s" % (e.args[0], e.em[e.args[0]])

回答1:


Here's a quick example of a custom Exception class with special codes:

class ErrorWithCode(Exception):
    def __init__(self, code):
        self.code = code
    def __str__(self):
        return repr(self.code)

try:
    raise ErrorWithCode(1000)
except ErrorWithCode as e:
    print("Received error with code:", e.code)

Since you were asking about how to use args here's an additional example...

class ErrorWithArgs(Exception):
    def __init__(self, *args):
        # *args is used to get a list of the parameters passed in
        self.args = [a for a in args]

try:
    raise ErrorWithArgs(1, "text", "some more text")
except ErrorWithArgs as e:
    print("%d: %s - %s" % (e.args[0], e.args[1], e.args[2]))



回答2:


This is an example of a custom exception I created which makes use of pre-defined error codes:

class CustomError(Exception):
"""
Custom Exception
"""

  def __init__(self, error_code, message='', *args, **kwargs):

      # Raise a separate exception in case the error code passed isn't specified in the ErrorCodes enum
      if not isinstance(error_code, ErrorCodes):
          msg = 'Error code passed in the error_code param must be of type {0}'
          raise CustomError(ErrorCodes.ERR_INCORRECT_ERRCODE, msg, ErrorCodes.__class__.__name__)

      # Storing the error code on the exception object
      self.error_code = error_code

      # storing the traceback which provides useful information about where the exception occurred
      self.traceback = sys.exc_info()

      # Prefixing the error code to the exception message
      try:
          msg = '[{0}] {1}'.format(error_code.name, message.format(*args, **kwargs))
      except (IndexError, KeyError):
          msg = '[{0}] {1}'.format(error_code.name, message)

      super().__init__(msg)


# Error codes for all module exceptions
@unique
class ErrorCodes(Enum):
    ERR_INCORRECT_ERRCODE = auto()      # error code passed is not specified in enum ErrorCodes
    ERR_SITUATION_1 = auto()            # description of situation 1
    ERR_SITUATION_2 = auto()            # description of situation 2
    ERR_SITUATION_3 = auto()            # description of situation 3
    ERR_SITUATION_4 = auto()            # description of situation 4
    ERR_SITUATION_5 = auto()            # description of situation 5
    ERR_SITUATION_6 = auto()            # description of situation 6

The enum ErrorCodes is used to define error codes. The exception is created in such a way that the error code passed is prefixed to the exception message.



来源:https://stackoverflow.com/questions/6180185/custom-python-exceptions-with-error-codes-and-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!