Skip to content

Commit 8645740

Browse files
committed
Add CLI exception message template
1 parent 1a0c8e0 commit 8645740

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Exceptions/BlunderSoftException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,4 @@
2222

2323
final class BlunderSoftException extends ErrorException
2424
{
25-
2625
}

src/Handlers/AbstractHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ abstract class AbstractHandler implements AbstractHandlerInterface
5959
*/
6060
public function setExitCode(int|null|bool $code): self
6161
{
62-
if(is_bool($code)) {
62+
if (is_bool($code)) {
6363
$code = ($code === false) ? null : 1;
6464
}
6565
self::$exitCode = $code;

src/Handlers/CliHandler.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function exceptionHandler(Throwable $exception): void
4949
* @param ExceptionItem|Throwable $exception
5050
* @return string
5151
*/
52-
protected function getErrorMessage(ExceptionItem|Throwable $exception): string
52+
public function getErrorMessage(ExceptionItem|Throwable $exception): string
5353
{
5454
if ($exception instanceof Throwable) {
5555
$exception = new ExceptionItem($exception);
@@ -70,7 +70,7 @@ protected function getErrorMessage(ExceptionItem|Throwable $exception): string
7070
}
7171

7272
$message = preg_replace('/[^\S\n]+/', ' ', (string)$exception->getMessage());
73-
if($exception->getException() instanceof BlunderSoftException) {
73+
if ($exception->getException() instanceof BlunderSoftException) {
7474
return self::ansi()->style(["bold", "red"], "Notice: ") . $message;
7575
}
7676
return sprintf(
@@ -86,6 +86,34 @@ protected function getErrorMessage(ExceptionItem|Throwable $exception): string
8686
)."\n";
8787
}
8888

89+
/**
90+
* Generate an small error message on one line
91+
*
92+
* @param ExceptionItem|Throwable $exception
93+
* @return string
94+
*/
95+
public function getSmallErrorMessage(ExceptionItem|Throwable $exception): string
96+
{
97+
if ($exception instanceof Throwable) {
98+
$exception = new ExceptionItem($exception);
99+
}
100+
101+
$msg = "\n";
102+
$msg .= self::ansi()->red("%s ") . self::ansi()->italic("(%s)") . ": ";
103+
$msg .= self::ansi()->bold("%s ");
104+
105+
$message = preg_replace('/[^\S\n]+/', ' ', (string)$exception->getMessage());
106+
if ($exception->getException() instanceof BlunderSoftException) {
107+
return self::ansi()->style(["bold", "red"], "Notice: ") . $message;
108+
}
109+
return sprintf(
110+
$msg,
111+
get_class($exception->getException()),
112+
(string)$exception->getSeverityConstant(),
113+
$message,
114+
);
115+
}
116+
89117
/**
90118
* Get trace result
91119
* @param array $traceArr

0 commit comments

Comments
 (0)