Print chained exceptions when maintenance script fails.
authordaniel <dkinzler@wikimedia.org>
Sun, 9 Dec 2018 21:22:17 +0000 (22:22 +0100)
committerDaniel Kinzler <dkinzler@wikimedia.org>
Mon, 10 Dec 2018 13:27:36 +0000 (13:27 +0000)
PHP supports exception chaining. This patch will output the error
message and stack trace not just for the latest exceptions, but for all
exceptions in the chain, using the Exception::getPrevious() method. This
avoids the problem where aftereffects obscure the actual problem, because
only the last exception in the chain was printed.

Change-Id: If577c5c89bb3b3e5766400fff07d8cc0a2d82610

maintenance/doMaintenance.php

index 1f1a4c7..816b385 100644 (file)
@@ -91,7 +91,17 @@ $maintenance->checkRequiredExtensions();
 $maintenance->setAgentAndTriggers();
 
 // Do the work
-$success = $maintenance->execute();
+try {
+       $success = $maintenance->execute();
+} catch ( Exception $ex ) {
+       $success = false;
+       while ( $ex ) {
+               $cls = get_class( $ex );
+               print "$cls from line {$ex->getLine()} of {$ex->getFile()}: {$ex->getMessage()}\n";
+               print $ex->getTraceAsString() . "\n";
+               $ex = $ex->getPrevious();
+       }
+}
 
 // Potentially debug globals
 $maintenance->globals();