TheFox / smtpd

SMTP server (library) for receiving emails, written in pure PHP.
https://fox21.at
MIT License
121 stars 30 forks source link

Exception handler #16

Open larsemil opened 5 years ago

larsemil commented 5 years ago

I would love to see a way to handle exceptions in a better way. I tried to implement it myself utilizing my own loop like this:

public function loop(){
        while(true) {
            try {
                $this->server->run();
            } catch (\Zend\Mail\Header\Exception\InvalidArgumentException $e){
                $this->log($e->getMessage());
                $this->log($e->getCode());
                $this->log($e->getFile().' line '.$e->getLine());

            }

        }
    }

But problem is that there is for now NO way to tell the client connecting that something went wrong. It would be totally awesome if we could register an method to handle all exceptions from the server, which also would send the client for which the exception occurred. It could look something like this.

$server->addExceptionHandler(function ($exception, $client){
    $server->logger('something went wrong')
    $client->sendSyntaxErrorInParameters();
});

All in all - awesome package. :) :+1:

TheFox commented 5 years ago

I like the idea. What do you expect to happen inside sendSyntaxErrorInParameters()? Which SMTP command should send the server to the client?

larsemil commented 5 years ago

With an exceptionhandler like that where the client that got the exception is existing you can do whatever you want with the client.

if the clients methods are public that is. :)

TheFox commented 5 years ago

I just want to figure out how this actually can happen. How can I reproduce an \Zend\Mail\Header\Exception\InvalidArgumentException?

TheFox commented 5 years ago

@larsemil Please re-test it with the newest version in master.

This is not exactly what you wanted, but InvalidArgumentExceptions will now be catched and sendSyntaxErrorInParameters() will be called.