Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #604 from phel-lang/feat/602-improve-error-message
Improve PHP notices and error messages
- Loading branch information
Showing
15 changed files
with
176 additions
and
59 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
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phel\Command\Domain\Handler; | ||
|
||
use Phel\Command\Domain\Shared\Exceptions\ExceptionPrinterInterface; | ||
use Phel\Compiler\Domain\Exceptions\CompilerException; | ||
use Throwable; | ||
|
||
final class ExceptionHandler | ||
{ | ||
private static string $previousNotice = ''; | ||
|
||
public function __construct( | ||
private ExceptionPrinterInterface $exceptionPrinter, | ||
) { | ||
} | ||
|
||
public function register(): void | ||
{ | ||
$this->registerExceptionHandler(); | ||
$this->registerNoticeHandler(); | ||
} | ||
|
||
private function registerExceptionHandler(): void | ||
{ | ||
set_exception_handler(function (Throwable $exception): void { | ||
if ($exception instanceof CompilerException) { | ||
$this->exceptionPrinter->printException($exception->getNestedException(), $exception->getCodeSnippet()); | ||
} else { | ||
$this->exceptionPrinter->printStackTrace($exception); | ||
} | ||
}); | ||
} | ||
|
||
private function registerNoticeHandler(): void | ||
{ | ||
set_error_handler(function ( | ||
int $errno, | ||
string $errstr, | ||
string $errfile = '', | ||
int $errline = 0, | ||
array $errcontext = [], | ||
): bool { | ||
$text = sprintf( | ||
"[%s] NOTICE found!\nmessage: \"%s\"\nfile: %s:%d\ncontext: %s", | ||
date(DATE_ATOM), | ||
$errstr, | ||
$errfile, | ||
$errline, | ||
json_encode(array_merge($errcontext, ['errno' => $errno])), | ||
); | ||
if (self::$previousNotice !== $text) { | ||
$this->exceptionPrinter->printError($text); | ||
} else { | ||
$this->exceptionPrinter->printError('and again'); | ||
} | ||
|
||
self::$previousNotice = $text; | ||
return true; | ||
}, E_NOTICE); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phel\Command\Domain\Shared\ErrorLog; | ||
|
||
final class ErrorLog implements ErrorLogInterface | ||
{ | ||
public function __construct( | ||
private string $filepath, | ||
) { | ||
} | ||
|
||
public function writeln(string $text): void | ||
{ | ||
file_put_contents( | ||
$this->filepath, | ||
$text . PHP_EOL, | ||
FILE_APPEND | LOCK_EX, | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/php/Command/Domain/Shared/ErrorLog/ErrorLogInterface.php
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phel\Command\Domain\Shared\ErrorLog; | ||
|
||
interface ErrorLogInterface | ||
{ | ||
public function writeln(string $text): void; | ||
} |
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
Oops, something went wrong.