Skip to content

Commit 46be6dc

Browse files
committed
Merge branch 'exception-exit-status'
2 parents 0e977f8 + aaeb6e8 commit 46be6dc

File tree

7 files changed

+56
-16
lines changed

7 files changed

+56
-16
lines changed

src/Cli/CliCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ abstract protected function getLongDescription(): ?string;
6666
* ```php
6767
* [
6868
* CliHelpSectionName::EXIT_STATUS => <<<EOF
69-
* `{{command}}` returns 0 when the operation succeeds, 1 when invalid arguments
70-
* are given, and 15 when an unhandled exception is thrown. Other non-zero values
69+
* `{{program}}` returns 0 when the operation succeeds, 1 when invalid arguments
70+
* are given, and 16 when an unhandled exception is thrown. Other non-zero values
7171
* may be returned for other failures.
7272
* EOF,
7373
* ];

src/Console/Support/ConsoleFormat.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public function apply(?string $text, array $attributes = []): string
5252
return '';
5353
}
5454

55-
// Remove indentation from the first line of code in fenced code blocks
56-
// to prevent doubling up
55+
// In fenced code blocks:
56+
// - remove indentation from the first line of code
57+
// - add a level of indentation to the block
5758
$tagId = $attributes[Attribute::TAG_ID] ?? null;
5859
if ($tagId === Tag::CODE_BLOCK) {
5960
$indent = $attributes[Attribute::INDENT] ?? '';
@@ -63,6 +64,7 @@ public function apply(?string $text, array $attributes = []): string
6364
$text = substr($text, $length);
6465
}
6566
}
67+
$text = ' ' . str_replace("\n", "\n ", $text);
6668
}
6769

6870
if ($this->Replace) {

src/Exception/Concern/ExceptionTrait.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@
1212
*/
1313
trait ExceptionTrait
1414
{
15+
protected ?int $ExitStatus = null;
16+
1517
public function __construct(
1618
string $message = '',
17-
?Throwable $previous = null
19+
?Throwable $previous = null,
20+
?int $exitStatus = null
1821
) {
22+
$this->ExitStatus = $exitStatus;
1923
parent::__construct($message, 0, $previous);
2024
}
2125

26+
/**
27+
* Set the exit status to return if the exception is not caught on the
28+
* command line
29+
*
30+
* @return static
31+
*/
32+
public function withExitStatus(?int $exitStatus)
33+
{
34+
$clone = clone $this;
35+
$clone->ExitStatus = $exitStatus;
36+
return $clone;
37+
}
38+
2239
/**
2340
* Get an array that maps names to formatted content
2441
*
@@ -29,6 +46,14 @@ public function getDetail(): array
2946
return [];
3047
}
3148

49+
/**
50+
* @inheritDoc
51+
*/
52+
public function getExitStatus(): ?int
53+
{
54+
return $this->ExitStatus;
55+
}
56+
3257
/**
3358
* @inheritDoc
3459
*/

src/Exception/Contract/ExceptionInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ interface ExceptionInterface extends Throwable
1212
* @return array<string,string>
1313
*/
1414
public function getDetail(): array;
15+
16+
/**
17+
* Get the exit status to return if the exception is not caught on the
18+
* command line
19+
*/
20+
public function getExitStatus(): ?int;
1521
}

src/Support/ErrorHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Lkrms\Contract\IFacade;
66
use Lkrms\Contract\ReceivesFacade;
7+
use Lkrms\Exception\Contract\ExceptionInterface;
78
use Lkrms\Facade\Console;
89
use Lkrms\Utility\File;
910
use Lkrms\Utility\Pcre;
@@ -29,7 +30,7 @@ final class ErrorHandler implements ReceivesFacade
2930
*/
3031
private array $Silenced = [];
3132

32-
private int $ExitStatus = 15;
33+
private int $ExitStatus = 16;
3334

3435
private bool $IsRegistered = false;
3536

@@ -197,6 +198,12 @@ public function handleException(Throwable $exception): void
197198
Console::exception($exception);
198199

199200
if (!$this->IsShuttingDown) {
201+
if ($exception instanceof ExceptionInterface) {
202+
$exitStatus = $exception->getExitStatus();
203+
if ($exitStatus !== null) {
204+
exit ($exitStatus);
205+
}
206+
}
200207
exit ($this->ExitStatus);
201208
}
202209
}

src/Sync/Command/CheckSyncProviderHeartbeat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getLongDescription(): ?string
9393
return
9494
($description ?? '') . <<<EOF
9595
If a heartbeat request fails, __{{command}}__ continues to the next
96-
provider unless --fail-early is given, in which case it exits
96+
provider unless `-f/--fail-early` is given, in which case it exits
9797
immediately.
9898
9999
The command exits with a non-zero status if a provider backend is

tests/unit/Console/ConsoleFormatterTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function formatProvider(): array
6868
*0* escapes \
6969
*1* with \
7070
*2* adjacent \
71-
*15* tags
71+
*16* tags
7272
EOF;
7373

7474
return [
@@ -84,19 +84,19 @@ public static function formatProvider(): array
8484
8585
Low-priority information with embedded tags.
8686
87-
<?php
88-
/**Preformatted code block**/
89-
$a = b($c);
87+
<?php
88+
/**Preformatted code block**/
89+
$a = b($c);
9090
9191
HEADING 3
9292
9393
Indented paragraph.
9494
9595
Indented _code span_
9696
97-
<?php
98-
/**Indented code block**/
99-
$a = b($c);
97+
<?php
98+
/**Indented code block**/
99+
$a = b($c);
100100
101101
Bold, italic and underline.
102102
@@ -108,7 +108,7 @@ public static function formatProvider(): array
108108
0 escapes
109109
1 with
110110
2 adjacent
111-
15 tags
111+
16 tags
112112
EOF,
113113
$default,
114114
$input,
@@ -153,7 +153,7 @@ public static function formatProvider(): array
153153
*0* escapes \
154154
*1* with \
155155
*2* adjacent \
156-
*15* tags
156+
*16* tags
157157
EOF,
158158
$loopback,
159159
$input,

0 commit comments

Comments
 (0)