Skip to content

Commit

Permalink
added ErrorHandlerHelper (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored May 21, 2018
1 parent 11b5304 commit 72ede49
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/SAREhub/Commons/Misc/ErrorHandlerHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php


namespace SAREhub\Commons\Misc;


use ErrorException;

class ErrorHandlerHelper
{
public static function registerDefaultErrorHandler()
{
self::enableErrorReporting(E_ALL);
self::hideDisplayErrors();
self::registerErrorToExceptionHandler(E_ALL);
}

public static function enableErrorReporting($errorTypes)
{
ini_set('error_reporting', $errorTypes);
}

public static function hideDisplayErrors()
{
ini_set('display_errors', "Off");
}

public static function registerErrorToExceptionHandler($errorTypes = E_ALL)
{
$handler = function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
};
set_error_handler($handler, $errorTypes);
}
}
17 changes: 17 additions & 0 deletions tests/SAREhub/Commons/Misc/ErrorHandlerHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace SAREhub\Commons\Misc;

use PHPUnit\Framework\TestCase;

class ErrorHandlerHelperTest extends TestCase
{

public function testConvertingErrorToException()
{
ErrorHandlerHelper::registerDefaultErrorHandler();
$this->expectException(\ErrorException::class);
$this->expectExceptionMessage("Undefined variable: undefined");
echo $undefined;
}
}

0 comments on commit 72ede49

Please sign in to comment.