2015-07-24 00:53:41 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Logging;
|
2015-07-24 00:53:41 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Email\Email;
|
2015-07-24 00:53:41 +02:00
|
|
|
use Monolog\Formatter\FormatterInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produce a friendly error message
|
|
|
|
*/
|
2015-10-12 06:34:34 +02:00
|
|
|
class DebugViewFriendlyErrorFormatter implements FormatterInterface {
|
2015-07-24 00:53:41 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Default status code
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
protected $statusCode = 500;
|
2015-10-12 06:34:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default friendly error
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
protected $friendlyErrorMessage = 'Error';
|
2015-10-12 06:34:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default error body
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
protected $friendlyErrorDetail;
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Get default status code
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
public function getStatusCode() {
|
|
|
|
return $this->statusCode;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Set default status code
|
|
|
|
*
|
|
|
|
* @param int $statusCode
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
public function setStatusCode($statusCode) {
|
|
|
|
$this->statusCode = $statusCode;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Get friendly title
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
2015-07-24 00:53:41 +02:00
|
|
|
return $this->friendlyErrorMessage;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Set friendly title
|
|
|
|
*
|
|
|
|
* @param string $title
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
public function setTitle($title) {
|
|
|
|
$this->friendlyErrorMessage = $title;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Get default error body
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBody() {
|
2015-07-24 00:53:41 +02:00
|
|
|
return $this->friendlyErrorDetail;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Set default error body
|
|
|
|
*
|
|
|
|
* @param string $body
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
public function setBody($body) {
|
|
|
|
$this->friendlyErrorDetail = $body;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
public function format(array $record) {
|
|
|
|
// Get error code
|
|
|
|
$code = empty($record['code']) ? $this->statusCode : $record['code'];
|
|
|
|
return $this->output($code);
|
2015-07-24 00:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function formatBatch(array $records) {
|
2015-10-12 06:34:34 +02:00
|
|
|
$message = '';
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$message .= $this->format($record);
|
|
|
|
}
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the appropriate error content for the given status code
|
|
|
|
*
|
|
|
|
* @param int $statusCode
|
|
|
|
* @return string Content in an appropriate format for the current request
|
|
|
|
*/
|
|
|
|
public function output($statusCode) {
|
2015-10-06 07:31:58 +02:00
|
|
|
// TODO: Refactor into a content-type option
|
2016-08-19 00:51:35 +02:00
|
|
|
if(Director::is_ajax()) {
|
2015-10-12 06:34:34 +02:00
|
|
|
return $this->getTitle();
|
2015-07-24 00:53:41 +02:00
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$renderer = Debug::create_debug_view();
|
2015-10-12 06:34:34 +02:00
|
|
|
$output = $renderer->renderHeader();
|
|
|
|
$output .= $renderer->renderInfo("Website Error", $this->getTitle(), $this->getBody());
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if(Email::config()->admin_email) {
|
|
|
|
$mailto = Email::obfuscate(Email::config()->admin_email);
|
2015-10-12 06:34:34 +02:00
|
|
|
$output .= $renderer->renderParagraph('Contact an administrator: ' . $mailto . '');
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= $renderer->renderFooter();
|
|
|
|
return $output;
|
2015-07-24 00:53:41 +02:00
|
|
|
}
|
|
|
|
}
|