2011-06-03 13:42
浏览 43


This user asked a similar question but had no viable solution in the answers, so here is am revisiting the topic.

I would like to have emails sent from custom exceptions in a clean way. I can do this easy with plain old errors from a custom error_handler, but with extended exceptions I am a bit stumped.

The main problem is I rely on Zend_Mail to have transparent cross-platform mailing, easy smtp/ssl configuration for gmail account usage and a bunch of other goodies. I would like to access my Zend_Mail object in a custom exception. Static calls are not really an option that I am willing to use, although it seems to be the easiest to use a 3rd party object in a custom exception. Here is the constructer of base exception class:

 public function __construct($message = null, $code = 0,
                             Exception $previous = null);

The only solution I think may be worthwhile at the time being is adding a parameter for mailer object in the extended exception class, yet I do not want to require all subclasses to pass in mailer object. The idea is that maybe this mailer obj. would be better off being an optional dependency.

__construct signature would then become:

      public function __construct($mailobj = null, $message = null, $code = 0,
                                  Exception $previous = null);  

Notice the order of parameters, only $code is required and it is in the middle of the signature! This poses another issue but that is for another day. My question for today is, does anyone have ideas / suggestions on how to deal with dependencies in custom exception classes? Keep in mind we are aiming for testable code.

Another easy option would have been to have the mailing code in a catch block, but that kind of defeats the purpose of using custom exceptions in my opinion, because the client of this class now has to think of adding mailing code in all catch blocks.

图片转代码服务由CSDN问答提供 功能建议

这个用户问了一个类似的问题,但答案中没有可行的解决方案,所以现在重新讨论这个话题。 / p>

我希望以干净的方式从自定义例外发送电子邮件。 我可以使用自定义error_handler中的普通旧错误轻松完成此操作,但是除了扩展异常之外,我有点难过。

主要问题是我依靠Zend_Mail来实现透明的跨平台邮件,轻松的smtp / ssl配置以及gmail帐户使用和其他一些好东西。 我想在自定义异常中访问我的Zend_Mail对象。 静态调用并不是我愿意使用的选项,尽管在自定义异常中使用第三方对象似乎最容易。 这是基本异常类的构造函数:

  public function __construct($ message = null,$ code = 0,
 Exception $ previous = null); 

我认为目前唯一值得考虑的解决方案是在扩展异常类中为邮件程序对象添加参数,但我不想要求所有子类都传入 邮件对象。 这个想法可能是这个邮件对象。 最好是一个可选的依赖。


  public function __construct($ mailobj = null,$ message = null,$ code = 0,  
例外$ previous = null);  

注意参数的顺序,只需要$ code,它就在签名的中间! 这带来了另一个问题,但这是另一天。 我今天的问题是,有没有人有关于如何处理自定义异常类中的依赖关系的想法/建议? 请记住,我们的目标是可测试的代码。

另一个简单的选择应该是将邮件代码放在catch块中,但是在我看来,这种方式违背了使用自定义异常的目的,因为 此类客户端现在必须考虑在所有catch块中添加邮件代码。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjizi9443 2011-06-03 13:51

    I would approach it like this (am I missing some requirement here?)

    class CustomException extends Exception // Zend_Exception?
        public function __construct($message = null, $code = 0, Exception $previous = null) {
            $mailobj = new Zend_Mail(/* ... */);
            // ....
            try {  // In order not to get infinitelly looped
            } catch(Exception $e) { }

    Whenever this exception is instantiated (i.e. throw new CustomException() ), someone will be receiving an email.

    打赏 评论

相关推荐 更多相似问题