Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Python Exceptions with Error Codes and Error Messages
    primarykey
    data
    text
    <pre><code>class AppError(Exception): pass class MissingInputError(AppError): pass class ValidationError(AppError): pass </code></pre> <p>...</p> <pre><code>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) </code></pre> <p>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.</p> <p>I'd like to define some custom error codes and error messages specific to my app like,</p> <blockquote> <p>Error 1100: "Key parameter not found. Please verify your input."</p> <p>Error 1101: "Date parameter not found. Please verify your input"</p> <p>...</p> <p>Error 2100: "Multiple Day parameter is not valid. Accepted values are 2, 5 and 7."</p> </blockquote> <p>and report the same to the user.</p> <ol> <li>How do I define these error codes and error messages in the custom exceptions?</li> <li>How do I raise / trap exception in a way that I know what error code / message to display?</li> </ol> <p><em>(P.S: This is for Python 2.4.3).</em></p> <hr> <p>Bastien Léonard mentions in this <a href="https://stackoverflow.com/questions/1877686/implementing-python-exceptions">SO comment</a> that you <strong>don't</strong> need to always define a new <code>__init__</code> or <code>__str__</code>; by default, arguments will be placed in self.args and they will be printed by <code>__str__</code>. </p> <p>Thus, the solution I prefer:</p> <pre><code>class AppError(Exception): pass class MissingInputError(AppError): # define the error codes &amp; 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."} </code></pre> <p>Usage:</p> <pre><code>try: # do something here that calls # raise MissingInputError(1101) except MissingInputError, e print "%d: %s" % (e.args[0], e.em[e.args[0]]) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload