3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Logger\LoggerFactory
;
22 use MediaWiki\Message\Message
;
25 * Show an error that looks like an HTTP server error.
26 * Replacement for wfHttpError().
33 class HttpError
extends MWException
{
36 /** @var string|Message|null */
38 /** @var string|Message */
43 * @param int $httpCode HTTP status code to send to the client
44 * @param string|Message $content Content of the message
45 * @param string|Message|null $header Content of the header (\<title\> and \<h1\>)
46 * @param-taint $content tainted
47 * @param-taint $header tainted
49 public function __construct( $httpCode, $content, $header = null ) {
50 parent
::__construct( $content );
51 $this->httpCode
= (int)$httpCode;
52 $this->header
= $header;
53 $this->content
= $content;
57 * We don't want the default exception logging as we got our own logging set
60 * @see MWException::isLoggable
65 public function isLoggable() {
70 * Returns the HTTP status code supplied to the constructor.
74 public function getStatusCode() {
75 return $this->httpCode
;
79 * Report and log the HTTP error.
80 * Sends the appropriate HTTP status code and outputs an
81 * HTML page with an error message.
83 public function report() {
86 HttpStatus
::header( $this->httpCode
);
87 header( 'Content-type: text/html; charset=utf-8' );
89 print $this->getHTML();
92 private function doLog() {
93 $logger = LoggerFactory
::getInstance( 'HttpError' );
94 $content = $this->content
;
96 if ( $content instanceof Message
) {
97 $content = $content->text();
101 'file' => $this->getFile(),
102 'line' => $this->getLine(),
103 'http_code' => $this->httpCode
,
106 $logMsg = "$content ({http_code}) from {file}:{line}";
108 if ( $this->getStatusCode() < 500 ) {
109 $logger->info( $logMsg, $context );
111 $logger->error( $logMsg, $context );
116 * Returns HTML for reporting the HTTP error.
117 * This will be a minimal but complete HTML document.
119 * @return string HTML
121 public function getHTML() {
122 if ( $this->header
=== null ) {
123 $titleHtml = htmlspecialchars( HttpStatus
::getMessage( $this->httpCode
) );
124 } elseif ( $this->header
instanceof Message
) {
125 $titleHtml = $this->header
->escaped();
127 $titleHtml = htmlspecialchars( $this->header
);
130 if ( $this->content
instanceof Message
) {
131 $contentHtml = $this->content
->escaped();
133 $contentHtml = nl2br( htmlspecialchars( $this->content
) );
136 return "<!DOCTYPE html>\n" .
137 "<html><head><title>$titleHtml</title><meta name=\"color-scheme\" content=\"light dark\" /></head>\n" .
138 "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";