Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exception handling - classic and fancy

Exception handling - classic and fancy

Exceptions are an integral part of PHP. PHP introduced them long ago, but handling has changed over the last versions. So the classic part contains a solid introduction to exception architecture, handling and how to use it. The fancy part will show some examples about modern exception logging.

Tommy Mühle

January 30, 2017
Tweet

More Decks by Tommy Mühle

Other Decks in Technology

Transcript

  1. Tommy Mühle | tommy-muehle.io Tommy Mühle | tommy-muehle.io 4 „An

    exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.“ https:/ /docs.oracle.com
  2. Tommy Mühle | tommy-muehle.io 8 /* @var $connection \Doctrine\DBAL\Connection */

    $connection->beginTransaction(); try { // ... $connection->commit(); } catch (\Exception $exception) { $connection->rollBack(); throw $exception; }
  3. /* @var $connection \Doctrine\DBAL\Connection */ $connection->beginTransaction(); try { // ...

    $connection->commit(); } catch (Doctrine\DBAL\DBALException $exception) { $connection->rollBack(); } catch (\Exception $exception) { // ... throw $exception; } Tommy Mühle | tommy-muehle.io 9
  4. Tommy Mühle | tommy-muehle.io 10 /* @var $connection \Doctrine\DBAL\Connection */

    $connection->beginTransaction(); try { // ... $connection->commit(); } catch (\Exception $exception) { $connection->rollBack(); throw $exception; } finally { /* @var $logger \Monolog\Logger */ $logger->info('...'); }
  5. Tommy Mühle | tommy-muehle.io 13 function my_handler($exception) { echo 'Got

    it: ' . $exception->getMessage(); } set_exception_handler('my_handler'); throw new Exception('Take this!');
  6. Tommy Mühle | tommy-muehle.io 21 function exception_error_handler ($severity, $message, $file,

    $line) { throw new ErrorException ($message, 0, $severity, $file, $line); } set_error_handler('exception_error_handler'); strpos(); // Trigger exception
  7. Tommy Mühle | tommy-muehle.io 27 try { // ... }

    catch (Error $error) { // ... } catch (Exception $exception) { // ... }
  8. try { // ... } catch (LengthException | OutOfRangeException $exception)

    { // ... } catch (Exception $exception) { // ... } Tommy Mühle | tommy-muehle.io 30
  9. Tommy Mühle | tommy-muehle.io 43 class FileNotReadableException extends RuntimeException {

    public function __construct(string $filename) { $message = sprintf('Cannot read %s!', $filename); $code = 666; parent::__construct($message, $code, null); } } Use specific codes
  10. Tommy Mühle | tommy-muehle.io 45 $url = 'unreachable.tld'; if (''

    === $url) { throw new InvalidUrlException('Blank url are invalid!'); } if ('' === parse_url($url, PHP_URL_SCHEME)) { $message = sprintf('The url %s has no scheme!', $url); throw new InvalidUrlException($message); } // ... Avoid inline message generation
  11. Tommy Mühle | tommy-muehle.io 46 class InvalidUrlException extends InvalidArgumentException {

    private static function create(string $reason) { return new static($reason); } public static function blankUrl() { return static::create('Blank url are invalid!'); } } Use named constructors
  12. Tommy Mühle | tommy-muehle.io 47 class InvalidUrlException extends InvalidArgumentException {

    // ... public static function blankUrl() { return static::create('Blank url are invalid!'); } public static function noScheme(string $url) { $reason = sprintf('The url %s has no scheme!', $url); return static::create($reason); } // ... } One method for each case
  13. Tommy Mühle | tommy-muehle.io 48 $url = 'unreachable.tld'; if (''

    === $url) { throw InvalidUrlException::blankUrl(); } if ('' === parse_url($url, PHP_URL_SCHEME)) { throw InvalidUrlException::noScheme($url); } // ...
  14. Tommy Mühle | tommy-muehle.io 51 try { // Code that

    may throw an Exception or Error. } catch (Throwable $exception) { // Executed only in PHP 7, // will not match in PHP 5.x } catch (Exception $exception) { // Executed only in PHP 5.x, // will not be reached in PHP 7 }
  15. Tommy Mühle | tommy-muehle.io Tommy Mühle | tommy-muehle.io 57 „Sentry

    is 100% open source. All features are built in the open and can be followed and contributed to on GitHub.“ https:/ /sentry.io/about
  16. Tommy Mühle | tommy-muehle.io 61 $sentryClient = new Raven_Client( 'https://<key>:<secret>@sentry.io/<project>'

    ); // or $sentryClient->install(); $error_handler = new Raven_ErrorHandler($sentryClient); $error_handler->registerExceptionHandler(); $error_handler->registerErrorHandler(); $error_handler->registerShutdownFunction();
  17. Tommy Mühle | tommy-muehle.io 62 $sentryClient->captureException($exception, [ 'extra' => [

    'php_version' => phpversion(), 'foo' => 'bar', // ... ], 'logger' => 'default', 'tags' => ['key' => 'value'], // group event by 'fingerprint' => ['{{ default }}', 'other value'], ]);