Added exception handler
This commit is contained in:
parent
8254a79c58
commit
f7c09cb7ff
|
@ -17,5 +17,10 @@
|
|||
"php": ">=5.4",
|
||||
"erusev/parsedown": "1.6.*",
|
||||
"twbs/bootstrap": "^3.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Engelsystem\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Engelsystem\Exceptions\Handler as ExceptionHandler;
|
||||
|
||||
/**
|
||||
* This file includes all needed functions, connects to the db etc.
|
||||
*/
|
||||
|
@ -97,6 +100,13 @@ require_once realpath(__DIR__ . '/../includes/pages/user_questions.php');
|
|||
require_once realpath(__DIR__ . '/../includes/pages/user_settings.php');
|
||||
require_once realpath(__DIR__ . '/../includes/pages/user_shifts.php');
|
||||
|
||||
$errorHandler = new ExceptionHandler(
|
||||
($environment == 'development'
|
||||
? ExceptionHandler::ENV_DEVELOPMENT
|
||||
: ExceptionHandler::ENV_PRODUCTION
|
||||
)
|
||||
);
|
||||
|
||||
session_start();
|
||||
|
||||
gettext_init();
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Handler
|
||||
{
|
||||
/** @var string */
|
||||
protected $environment;
|
||||
|
||||
const ENV_PRODUCTION = 'prod';
|
||||
const ENV_DEVELOPMENT = 'dev';
|
||||
const ENV_DEBUGGING = 'debug';
|
||||
|
||||
/**
|
||||
* Handler constructor.
|
||||
*
|
||||
* @param string $environment production|development
|
||||
*/
|
||||
public function __construct($environment = self::ENV_PRODUCTION)
|
||||
{
|
||||
$this->environment = $environment;
|
||||
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
set_exception_handler([$this, 'exceptionHandler']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
* @param string $string
|
||||
* @param string $file
|
||||
* @param int $line
|
||||
* @param array $context
|
||||
*/
|
||||
public function errorHandler($number, $string, $file, $line, $context)
|
||||
{
|
||||
$this->handle('error', $number, $string, $file, $line, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $e
|
||||
*/
|
||||
public function exceptionHandler(Exception $e)
|
||||
{
|
||||
$this->handle(
|
||||
'exception',
|
||||
$e->getCode(),
|
||||
get_class($e) . ': ' . $e->getMessage(),
|
||||
$e->getFile(),
|
||||
$e->getLine()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param int $number
|
||||
* @param string $string
|
||||
* @param string $file
|
||||
* @param int $line
|
||||
* @param array $context
|
||||
*/
|
||||
protected function handle($type, $number, $string, $file, $line, $context = [])
|
||||
{
|
||||
error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s',
|
||||
$type,
|
||||
$number,
|
||||
$string,
|
||||
$file,
|
||||
$line,
|
||||
json_encode($context)
|
||||
));
|
||||
|
||||
if ($this->environment == self::ENV_DEVELOPMENT || $this->environment == self::ENV_DEBUGGING) {
|
||||
echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;overflow-y:auto;">';
|
||||
echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
|
||||
var_export([
|
||||
'string' => $string,
|
||||
'file' => $file . ':' . $line,
|
||||
'context' => ($this->environment == self::ENV_DEBUGGING ? $context : null),
|
||||
]);
|
||||
echo '</pre>';
|
||||
die();
|
||||
}
|
||||
|
||||
echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $environment
|
||||
*/
|
||||
public function setEnvironment($environment)
|
||||
{
|
||||
$this->environment = $environment;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue