<?php
class ExHandler {
public static function handle(Exception $e) {
$label = 'Uncaught';
foreach(self::generate($e) as $cur) {
printf("%s %s: %s\nStack Trace:\n%s\n\n",
$label, get_class($cur), $cur->getMessage(),
preg_replace('/^/m', ' ', $cur->getTraceAsString())
);
$label = 'Next';
}
printf("Thrown in %s on line %d\n", $e->getFile(), $e->getLine());
printf("Custom shit over\n");
set_exception_handler(null);
throw $e;
}
protected static function generate(Exception $e) {
$prev = $e->getPrevious();
if( ! is_null($prev) ) {
foreach( self::generate($prev) as $cur ) {
yield $cur;
}
}
yield $e;
}
}
class Foo {
public function bork() {
$first = new Exception('I happened first!');
$second = new Exception('I happened second!', 0, $first);
throw new Exception('fission mailed', 0, $second);
}
}
set_exception_handler(['ExHandler', 'handle']);
(new Foo())->bork();
/*
Fatal error: Uncaught Exception: I happened earlier! in /in/F4B7q:5
Stack trace:
#0 /in/F4B7q(10): Foo->bork()
#1 {main}
Next Exception: fission mailed in /in/F4B7q:6
Stack trace:
#0 /in/F4B7q(10): Foo->bork()
#1 {main}
thrown in /in/F4B7q on line 6
*/
preferences:
60.5 ms | 402 KiB | 5 Q