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

115 lines
2.9 KiB
PHP
Raw Permalink 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.
*/
2016-12-04 16:33:52 +00:00
public static function register(): void
2016-10-07 17:55:50 +00:00
{
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
*/
2016-12-04 16:33:52 +00:00
public static function exception(Throwable $ex): void
2016-08-05 19:13:55 +00:00
{
2016-12-04 17:01:29 +00:00
$report = config('dev.report_host');
2016-08-05 19:13:55 +00:00
2016-12-04 17:01:29 +00:00
if (strlen($report) > 0) {
2016-08-05 19:13:55 +00:00
self::report($ex, $report);
}
2017-04-07 15:26:52 +00:00
self::view($ex, $report !== null);
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
2016-12-04 16:33:52 +00:00
* @throws ErrorException
2016-08-05 19:13:55 +00:00
*/
2016-12-04 16:33:52 +00:00
public static function error(int $severity, string $message, string $file, int $line): void
2016-08-05 19:13:55 +00:00
{
throw new ErrorException($message, 0, $severity, $file, $line);
}
/**
* Display an error message.
* @param Throwable $ex
* @param bool $reported
*/
2016-12-04 16:33:52 +00:00
private static function view(Throwable $ex, bool $reported = false): void
2016-08-05 19:13:55 +00:00
{
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
*/
2016-12-04 16:33:52 +00:00
private static function report(Throwable $ex, string $destination): void
2016-08-05 19:13:55 +00:00
{
$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));
}
}