This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/ExceptionHandler.php

114 lines
2.8 KiB
PHP
Raw Normal View History

2016-08-05 19:13:55 +00:00
<?php
/**
* Holds the exception (and error) handler.
* @package Sakura
*/
namespace Sakura;
use ErrorException;
use stdClass;
use Throwable;
/**
* Exception handler.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class ExceptionHandler
{
/**
* Disables checking if the templating engine is available
* @var bool
*/
private static $disableTemplate = false;
2016-10-07 17:55:50 +00:00
/**
* Register as the error and exception handler.
*/
public static function register()
{
set_exception_handler([static::class, 'exception']);
set_error_handler([static::class, 'error']);
}
2016-08-05 19:13:55 +00:00
/**
* The entry point for set_exception_handler.
* @param Throwable $ex
*/
public static function exception(Throwable $ex)
{
2016-11-06 22:19:35 +00:00
$report = strlen(config('dev.report_host')) > 0;
2016-08-05 19:13:55 +00:00
2016-11-06 22:19:35 +00:00
if ($report) {
2016-08-05 19:13:55 +00:00
self::report($ex, $report);
}
2016-11-06 22:19:35 +00:00
self::view($ex, $report);
2016-08-05 19:13:55 +00:00
}
/**
* The entry point for set_error_handler.
* @param int $severity
* @param string $message
* @param string $file
* @param int $line
*/
public static function error($severity, $message, $file, $line)
{
throw new ErrorException($message, 0, $severity, $file, $line);
}
/**
* Display an error message.
* @param Throwable $ex
* @param bool $reported
*/
private static function view(Throwable $ex, $reported = false)
{
http_response_code(500);
2016-09-12 11:13:05 +00:00
$debug = config('dev.show_errors');
$cli = php_sapi_name() === 'cli';
if ($cli || $debug) {
if (!$cli) {
header('Content-Type: text/plain');
}
2016-08-05 19:13:55 +00:00
echo $ex;
} else {
if (!self::$disableTemplate && Template::available()) {
echo view('errors/500', compact('reported'));
} else {
// Disable templates permanently so we don't get stuck in an infinite loop
// if an exception is caused by the templating engine.
self::$disableTemplate = true;
echo "Something broke so badly that the error page failed to render.";
if ($reported) {
echo " The developers have been notified of the issue.";
}
}
}
}
/**
* Posts the exception as json to a remote server.
* @param Throwable $ex
* @param string $destination
*/
private static function report(Throwable $ex, $destination)
{
$send = new stdClass;
$send->Date = date('c');
$send->Message = $ex->getMessage();
$send->Code = $ex->getCode();
$send->File = $ex->getFile();
$send->Line = $ex->getLine();
$send->Trace = $ex->getTraceAsString();
Net::request($destination, 'POST', json_encode($send));
}
}