Merge "OOUI theme support"
[lhc/web/wiklou.git] / includes / exception / HttpError.php
index 6ab6e03..d3ee9b9 100644 (file)
@@ -18,6 +18,8 @@
  * @file
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Show an error that looks like an HTTP server error.
  * Replacement for wfHttpError().
@@ -33,7 +35,7 @@ class HttpError extends MWException {
         *
         * @param int $httpCode HTTP status code to send to the client
         * @param string|Message $content Content of the message
-        * @param string|Message $header Content of the header (\<title\> and \<h1\>)
+        * @param string|Message|null $header Content of the header (\<title\> and \<h1\>)
         */
        public function __construct( $httpCode, $content, $header = null ) {
                parent::__construct( $content );
@@ -42,6 +44,19 @@ class HttpError extends MWException {
                $this->content = $content;
        }
 
+       /**
+        * We don't want the default exception logging as we got our own logging set
+        * up in self::report.
+        *
+        * @see MWException::isLoggable
+        *
+        * @since 1.24
+        * @return bool
+        */
+       public function isLoggable() {
+               return false;
+       }
+
        /**
         * Returns the HTTP status code supplied to the constructor.
         *
@@ -52,11 +67,13 @@ class HttpError extends MWException {
        }
 
        /**
-        * Report the HTTP error.
+        * Report and log the HTTP error.
         * Sends the appropriate HTTP status code and outputs an
         * HTML page with an error message.
         */
        public function report() {
+               $this->doLog();
+
                $httpMessage = HttpStatus::getMessage( $this->httpCode );
 
                header( "Status: {$this->httpCode} {$httpMessage}", true, $this->httpCode );
@@ -65,6 +82,29 @@ class HttpError extends MWException {
                print $this->getHTML();
        }
 
+       private function doLog() {
+               $logger = LoggerFactory::getInstance( 'HttpError' );
+               $content = $this->content;
+
+               if ( $content instanceof Message ) {
+                       $content = $content->text();
+               }
+
+               $context = array(
+                       'file' => $this->getFile(),
+                       'line' => $this->getLine(),
+                       'http_code' => $this->httpCode,
+               );
+
+               $logMsg = "$content ({http_code}) from {file}:{line}";
+
+               if ( $this->getStatusCode() < 500 ) {
+                       $logger->info( $logMsg, $context );
+               } else {
+                       $logger->error( $logMsg, $context );
+               }
+       }
+
        /**
         * Returns HTML for reporting the HTTP error.
         * This will be a minimal but complete HTML document.
@@ -73,21 +113,21 @@ class HttpError extends MWException {
         */
        public function getHTML() {
                if ( $this->header === null ) {
-                       $header = HttpStatus::getMessage( $this->httpCode );
+                       $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
                } elseif ( $this->header instanceof Message ) {
-                       $header = $this->header->escaped();
+                       $titleHtml = $this->header->escaped();
                } else {
-                       $header = htmlspecialchars( $this->header );
+                       $titleHtml = htmlspecialchars( $this->header );
                }
 
                if ( $this->content instanceof Message ) {
-                       $content = $this->content->escaped();
+                       $contentHtml = $this->content->escaped();
                } else {
-                       $content = htmlspecialchars( $this->content );
+                       $contentHtml = htmlspecialchars( $this->content );
                }
 
                return "<!DOCTYPE html>\n" .
-               "<html><head><title>$header</title></head>\n" .
-               "<body><h1>$header</h1><p>$content</p></body></html>\n";
+               "<html><head><title>$titleHtml</title></head>\n" .
+               "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";
        }
 }