Page 1 of 1

Error checking the objects on instance creating

Posted: Wed Mar 25, 2009 1:26 pm
by Technocrat
First let me say wonderful job. Always loved this project and the new version is outstanding.

I miss read your directions in the manual and didn't make a Swift_MailTransport::newInstance to go with the Swift_Mailer::newInstance. What happened was it gave me an error saying that IsStarted could not be found. After a few wtf minutes I figured it out. But that brings up my suggestion, that perhaps you should validate the instances that get passed in when making a new instance or at least check to make sure that something is there. Because the error you get when you don't doesn't make sense in the contents of what you are doing.

Anyways nice work.

Re: Error checking the objects on instance creating

Posted: Wed Mar 25, 2009 5:21 pm
by Chris Corbyn
I'm not 100% I understand what you mean. Can you post some code that revealed an error? Swift Mailer does type-hint on its parameters so you shouldbnn't have been able to pass the wrong instance to say "new Swift_Mailer( $badInstance ).

Thanks for the compliments on the work :)

Re: Error checking the objects on instance creating

Posted: Wed Mar 25, 2009 5:33 pm
by Technocrat
Notice I made a new Mailer instance without a transport.

Code: Select all

    private function _send() {
        //Swiftmailer
        include_once(_INCLUDES.'swiftmailer/swift_required.php');
 
        $mailer = Swift_Mailer::newInstance();
        $message = Swift_Message::newInstance();
        $message->setSubject($this->subject);
        $message->setBody($this->message, 'text/html');
        $message->setFrom(array($this->from => $this->from_name));
        $message->setTo($this->to);
        $mailer->send($message);
    }
So it makes it all the way to $mailer->send($message); without an error. Then that errors saying the the line:

Code: Select all

if (!$this->_transport->isStarted())
Could not run because isStarted isn't part of the object. Well obviously because I stupidly didn't make a transport object :banghead:

So the problem is that (at least for me) when newInstance runs it assumes that the passed in transport object is valid and just assigns it. When it probably should be validated a bit more before it gets to that point to keep people like me from hurting ourselves. :wink:

Re: Error checking the objects on instance creating

Posted: Wed Mar 25, 2009 9:41 pm
by Chris Corbyn
That's interesting, and you never got an error?

Code: Select all

 /**
   * Create a new Mailer instance.
   * 
   * @param Swift_Transport $transport
   * @return Swift_Mailer
   */
  public static function newInstance(Swift_Transport $transport)
  {
    return new self($transport);
  }
PHP should be fatal erroring on that. Beyond Swift Mailer's control really unless I follow every type-hinted method with an instanceof check, which sort of seems redundant.

Do you have full error reporting on?

Re: Error checking the objects on instance creating

Posted: Thu Mar 26, 2009 11:28 am
by Technocrat
I figured it out finally. I had the function being used with set_error_handler not returning false, thus it was hiding the error. Sorry about that.