-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGECLOUD-1193: Enable strict errors (#71)
- Loading branch information
1 parent
29df844
commit 979a25c
Showing
9 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
error_reporting(E_ALL); | ||
date_default_timezone_set('UTC'); | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$handler = new \Magento\MagentoCloud\App\ErrorHandler(); | ||
set_error_handler([$handler, 'handle']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\MagentoCloud\App; | ||
|
||
/** | ||
* An error handler that converts runtime errors into exceptions. | ||
*/ | ||
class ErrorHandler | ||
{ | ||
/** | ||
* Error messages | ||
* | ||
* @var array | ||
*/ | ||
private $errorPhrases = [ | ||
E_ERROR => 'Error', | ||
E_WARNING => 'Warning', | ||
E_PARSE => 'Parse Error', | ||
E_NOTICE => 'Notice', | ||
E_CORE_ERROR => 'Core Error', | ||
E_CORE_WARNING => 'Core Warning', | ||
E_COMPILE_ERROR => 'Compile Error', | ||
E_COMPILE_WARNING => 'Compile Warning', | ||
E_USER_ERROR => 'User Error', | ||
E_USER_WARNING => 'User Warning', | ||
E_USER_NOTICE => 'User Notice', | ||
E_STRICT => 'Strict Notice', | ||
E_RECOVERABLE_ERROR => 'Recoverable Error', | ||
E_DEPRECATED => 'Deprecated Functionality', | ||
E_USER_DEPRECATED => 'User Deprecated Functionality', | ||
]; | ||
|
||
/** | ||
* Custom error handler. | ||
* | ||
* @param int $errorNo | ||
* @param string $errorStr | ||
* @param string $errorFile | ||
* @param int $errorLine | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public function handle($errorNo, $errorStr, $errorFile, $errorLine) | ||
{ | ||
if (strpos($errorStr, 'DateTimeZone::__construct') !== false) { | ||
/** | ||
* There's no way to distinguish between caught system exceptions and warnings. | ||
*/ | ||
return false; | ||
} | ||
|
||
$errorNo = $errorNo & error_reporting(); | ||
|
||
if ($errorNo == 0) { | ||
return false; | ||
} | ||
|
||
$msg = isset($this->errorPhrases[$errorNo]) ? $this->errorPhrases[$errorNo] : "Unknown error ({$errorNo})"; | ||
$msg .= ": {$errorStr} in {$errorFile} on line {$errorLine}"; | ||
|
||
throw new \Exception($msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\MagentoCloud\Test\Unit\App; | ||
|
||
use Magento\MagentoCloud\App\ErrorHandler; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class ErrorHandlerTest extends TestCase | ||
{ | ||
/** | ||
* @var ErrorHandler | ||
*/ | ||
private $handler; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->handler = new ErrorHandler(); | ||
} | ||
|
||
public function testHandleDatetime() | ||
{ | ||
$this->assertFalse( | ||
$this->handler->handle(1, 'DateTimeZone::__construct', 'some_file.php', 1) | ||
); | ||
} | ||
|
||
public function testHandleNoError() | ||
{ | ||
$this->assertFalse( | ||
$this->handler->handle(0, 'Some string', 'some_file.php', 1) | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Error: Some string in some_file.php on line 1 | ||
*/ | ||
public function testHandleWithException() | ||
{ | ||
$this->handler->handle(1, 'Some string', 'some_file.php', 1); | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Unknown error (3): Some string in some_file.php on line 1 | ||
*/ | ||
public function testHandleWithUnknownException() | ||
{ | ||
$this->handler->handle(3, 'Some string', 'some_file.php', 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters