and \
)
*/
public function __construct( $httpCode, $content, $header = null ) {
parent::__construct( $content );
$this->httpCode = (int)$httpCode;
$this->header = $header;
$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.
*
* @return int
*/
public function getStatusCode() {
return $this->httpCode;
}
/**
* 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();
HttpStatus::header( $this->httpCode );
header( 'Content-type: text/html; charset=utf-8' );
print $this->getHTML();
}
private function doLog() {
$logger = LoggerFactory::getInstance( 'HttpError' );
$content = $this->content;
if ( $content instanceof Message ) {
$content = $content->text();
}
$context = [
'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.
*
* @return string HTML
*/
public function getHTML() {
if ( $this->header === null ) {
$titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
} elseif ( $this->header instanceof Message ) {
$titleHtml = $this->header->escaped();
} else {
$titleHtml = htmlspecialchars( $this->header );
}
if ( $this->content instanceof Message ) {
$contentHtml = $this->content->escaped();
} else {
$contentHtml = nl2br( htmlspecialchars( $this->content ) );
}
return "\n" .
"$titleHtml\n" .
"$titleHtml
$contentHtml
\n";
}
}