Custom Exception Messages: Best practices

前端 未结 5 1680
無奈伤痛
無奈伤痛 2020-12-22 17:27

Wondering how much effort I should go to forcing useful debugging information when creating exception messages, or should I just trust the user to supply the right info, or

相关标签:
5条回答
  • 2020-12-22 17:33

    I strongly recommend the advice on Krzysztof's blog and would note that in your case you seem to be trying to deal with what he calls Usage Errors.

    In this case what is required is not a new type to indicate it but a better error message about what caused it. As such a helper function to either:

    1. generate the textual string to place into the exception
    2. generate the whole exception and message

    Is what is required.

    Approach 1 is clearer, but may lead to a little more verbose usage, 2 is the opposite, trading a terser syntax for less clarity.

    Note that the functions must be extremely safe (they should never, ever cause an unrelated exception themselves) and not force the provision of data that is optional in certain reasonable uses.

    By using either of these approaches you make it easier to internationalise the error message later if required.

    A stack trace at a minimum gives you the function, and possibly the line number, thus you should focus on supplying information that is not easy to work out from that.

    0 讨论(0)
  • 2020-12-22 17:39

    See How to Design Exception Hierarchies on the blog of Krzysztof Cwalina, a coauthor of "Framework Design Guidelines".

    0 讨论(0)
  • 2020-12-22 17:43

    Never, ever trust a user to 'do the right thing', and include information for debugging. If you want information, you need to gather it yourself and store it somewhere where its accessible.

    Also as stated, if it's hard(er) to do something, the users will avoid doing it, so again, don't depend on their goodwill and their knowledge of what they need to send.

    This thinking implies a method by which you collect the information and log it, which implies using var_dump() somewhere.

    Also, as said by Mark Harrison, a button which makes it easy to send an error message somewhere is fantastic for you and for the users. It makes it easy for them to report an error. You (as the recipient) get a lot of duplicates, but duplicate information is better than no information.

    0 讨论(0)
  • 2020-12-22 17:44

    However much detail you add, be sure and either

    • make it easy to cut and paste the whole thing, or
    • have a button that will report the error for them
    0 讨论(0)
  • 2020-12-22 17:50

    I won't detract from the advise regarding Krzysztof's blog, but here is a dead-easy way to create custom exceptions.

    Example:

    <?php
       require_once "CustomException.php";
       class SqlProxyException extends CustomException {}
    
       throw new SqlProxyException($errorMsg, mysql_errno());     
    ?>
    

    The code behind that (which I borrowed somewhere, apologies to whomever that was)

    <?php
    
    interface IException
    {
        /* Protected methods inherited from Exception class */
        public function getMessage();                 // Exception message
        public function getCode();                    // User-defined Exception code
        public function getFile();                    // Source filename
        public function getLine();                    // Source line
        public function getTrace();                   // An array of the backtrace()
        public function getTraceAsString();           // Formated string of trace
    
        /* Overrideable methods inherited from Exception class */
        public function __toString();                 // formated string for display
        public function __construct($message = null, $code = 0);
    }
    
    abstract class CustomException extends Exception implements IException
    {
        protected $message = 'Unknown exception';     // Exception message
        private   $string;                            // Unknown
        protected $code    = 0;                       // User-defined exception code
        protected $file;                              // Source filename of exception
        protected $line;                              // Source line of exception
        private   $trace;                             // Unknown
    
        public function __construct($message = null, $code = 0)
        {
            if (!$message) {
                throw new $this('Unknown '. get_class($this));
            }
            parent::__construct($message, $code);
        }
    
        public function __toString()
        {
            return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})\n"
                                    . "{$this->getTraceAsString()}";
        }
    }
    
    0 讨论(0)
提交回复
热议问题