OpenBuildings / postmark

Postmark transport for Swift Mailer
https://github.com/OpenBuildings/postmark
BSD 3-Clause "New" or "Revised" License
16 stars 8 forks source link

Transport triggers `exceptionThrown` event when sending fails. Transport can have an event listener. #32

Closed jxmallett closed 8 years ago

jxmallett commented 8 years ago

Thanks for your work on this. Actually using the postmark API makes error handling much easier!

I've added some code to have the exceptionThrown Swift event triggered when an error occurs with the API call.

In order to catch Swift events, I've also added an option to connect an event listener to the Transport.

In the case of exceptionThrown events, the message is added to the Transport so it can be accessed from the event listener.

Here's the event listener I use:

use Openbuildings\Postmark\Swift_Transport_PostmarkTransport;

class SwiftmailerListener implements \Swift_Events_TransportExceptionListener
{
    /**
     * Invoked as a TransportException is thrown in the Transport system.
     *
     * @param \Swift_Events_TransportExceptionEvent $evt
     */
    public function exceptionThrown(\Swift_Events_TransportExceptionEvent $evt)
    {
        $source = $evt->getSource();
        if (!($source instanceof Swift_Transport_PostmarkTransport)) {
            //Not a great deal we can do because we don't have access to the message
            return;
        }

        $message = $source->getMessage();

        //Log the error and maybe inform the sender?
    }
}

Finally, in looking through the code and making my changes, I added leading slashes to some of the namespacing to quiet phpStorm.

Let me know if there's anything you'd like me to change or clarify.

hkdobrev commented 8 years ago

Could you squash the last 3 commits into one? Ultimately the 1st commit would be a separate PR as it could be released as 0.3.x. I consider the other changes related to the exception handling to be breaking backwards compatibility as for users who have used the event dispatcher, we haven't been emitting the exceptionThrown event until now.

For squashing your last 3 commits you could use:

git reset --soft HEAD~3 &&
git commit

or git rebase --interactive if you know what you are doing.