Catch excpt to avoid fatal in Message::__toString
authordaniel <daniel.kinzler@wikimedia.de>
Mon, 7 Jan 2013 13:48:04 +0000 (14:48 +0100)
committerdaniel <daniel.kinzler@wikimedia.de>
Tue, 8 Jan 2013 16:55:31 +0000 (17:55 +0100)
PHP doesn't allow __toString to throw exceptions and will
trigger a fatal error if it does. So, catch any exceptions
and try to report them using wfWarn.

Change-Id: I52d38e9a927da4db18f4733fd207f9396adedf94

includes/Message.php

index 976f144..8f10b8b 100644 (file)
@@ -480,7 +480,24 @@ class Message {
         * @return String
         */
        public function __toString() {
-               return $this->toString();
+               // PHP doesn't allow __toString to throw exceptions and will
+               // trigger a fatal error if it does. So, catch any exceptions.
+
+               try {
+                       return $this->toString();
+               } catch ( Exception $ex ) {
+                       try {
+                               trigger_error( "Exception caught in " . __METHOD__ . " (message " . $this->key . "): "
+                                       . $ex, E_USER_WARNING );
+                       } catch ( Exception $ex ) {
+                               // Doh! Cause a fatal error after all?
+                       }
+
+                       if ( $this->format === 'plain' ) {
+                               return '<' . $this->key . '>';
+                       }
+                       return '&lt;' . $this->key . '&gt;';
+               }
        }
 
        /**