Skip to content

Commit a9268a2

Browse files
committed
fix: improve exception handling
1 parent bc08125 commit a9268a2

File tree

5 files changed

+246
-8
lines changed

5 files changed

+246
-8
lines changed

src/LibraryStarterKit/Setup.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,20 @@ class Setup
4646
private Filesystem $filesystem;
4747
private Finder $finder;
4848
private Project $project;
49+
private int $verbosity;
4950

5051
public function __construct(
5152
Project $project,
5253
Event $event,
5354
Filesystem $filesystem,
54-
Finder $finder
55+
Finder $finder,
56+
int $verbosity
5557
) {
5658
$this->project = $project;
5759
$this->event = $event;
5860
$this->filesystem = $filesystem;
5961
$this->finder = $finder;
62+
$this->verbosity = $verbosity;
6063
}
6164

6265
/**
@@ -107,6 +110,14 @@ public function getProject(): Project
107110
return $this->project;
108111
}
109112

113+
/**
114+
* Returns the verbosity level
115+
*/
116+
public function getVerbosity(): int
117+
{
118+
return $this->verbosity;
119+
}
120+
110121
/**
111122
* Returns an instance used for executing a system command
112123
*

src/LibraryStarterKit/Wizard.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\Console\Question\Question;
3737
use Symfony\Component\Console\Style\SymfonyStyle;
3838
use Symfony\Component\Finder\Finder;
39+
use Throwable;
3940

4041
use function basename;
4142
use function dirname;
@@ -78,6 +79,8 @@ public function getSetup(): Setup
7879

7980
protected function execute(InputInterface $input, OutputInterface $output): int
8081
{
82+
$output->setVerbosity($this->getSetup()->getVerbosity());
83+
8184
$console = $this->styleFactory->factory($input, $output);
8285
$console->title('Welcome to the PHP Library Starter Kit!');
8386
$console->block(
@@ -89,12 +92,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8992

9093
$this->registerInterruptHandler($console);
9194

92-
if (!$this->confirmStart($console)) {
93-
return 0;
94-
}
95+
try {
96+
if (!$this->confirmStart($console)) {
97+
return 0;
98+
}
9599

96-
$this->askQuestions($console);
97-
$this->setup->run($console, $this->answers);
100+
$this->askQuestions($console);
101+
$this->setup->run($console, $this->answers);
102+
} catch (Throwable $throwable) {
103+
return $this->handleException($throwable, $console);
104+
}
98105

99106
$console->success([
100107
sprintf('Congratulations! Your project, %s, is ready!', (string) $this->answers->packageName),
@@ -173,6 +180,30 @@ private function registerInterruptHandler(SymfonyStyle $console): void
173180
// phpcs:enable
174181
}
175182

183+
private function handleException(Throwable $throwable, SymfonyStyle $console): int
184+
{
185+
$errorMessages = [
186+
$throwable->getMessage(),
187+
sprintf('At line %d in %s', $throwable->getLine(), $throwable->getFile()),
188+
];
189+
190+
if ($console->getVerbosity() === OutputInterface::VERBOSITY_DEBUG) {
191+
$errorMessages[] = $throwable->getTraceAsString();
192+
}
193+
194+
$console->error($errorMessages);
195+
196+
$console->block([
197+
'Oops! I encountered an error.',
198+
'Please go here and click the "New issue" button to report this error: '
199+
. 'https://github.com/ramsey/php-library-starter-kit/issues',
200+
]);
201+
202+
$console->newLine();
203+
204+
return (int) $throwable->getCode() ?: 1;
205+
}
206+
176207
public static function newApplication(): Application
177208
{
178209
return self::$application ?? new Application();
@@ -186,7 +217,7 @@ public static function start(Event $event): void
186217
$projectName = (string) preg_replace('/[^a-z0-9]/', '-', $projectName);
187218

188219
$project = new Project($projectName, $appPath);
189-
$setup = new Setup($project, $event, new Filesystem(), new Finder());
220+
$setup = new Setup($project, $event, new Filesystem(), new Finder(), self::determineVerbosityLevel($event));
190221

191222
$command = new self($setup);
192223

@@ -196,4 +227,17 @@ public static function start(Event $event): void
196227

197228
$application->run(new StringInput('starter-kit'));
198229
}
230+
231+
public static function determineVerbosityLevel(Event $event): int
232+
{
233+
if ($event->getIO()->isDebug()) {
234+
return OutputInterface::VERBOSITY_DEBUG;
235+
} elseif ($event->getIO()->isVeryVerbose()) {
236+
return OutputInterface::VERBOSITY_VERY_VERBOSE;
237+
} elseif ($event->getIO()->isVerbose()) {
238+
return OutputInterface::VERBOSITY_VERBOSE;
239+
}
240+
241+
return OutputInterface::VERBOSITY_NORMAL;
242+
}
199243
}

tests/LibraryStarterKit/SetupTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Ramsey\Dev\LibraryStarterKit\Project;
1212
use Ramsey\Dev\LibraryStarterKit\Setup;
1313
use Ramsey\Dev\LibraryStarterKit\Task\Build;
14+
use Symfony\Component\Console\Output\OutputInterface;
1415
use Symfony\Component\Console\Style\SymfonyStyle;
1516
use Symfony\Component\Finder\Finder;
1617
use Symfony\Component\Process\Process;
@@ -49,6 +50,7 @@ public function setUp(): void
4950
$event,
5051
$filesystem,
5152
$finder,
53+
OutputInterface::VERBOSITY_NORMAL,
5254
);
5355
}
5456

@@ -77,6 +79,11 @@ public function testGetProjectName(): void
7779
$this->assertSame('a-project-name', $this->setup->getProjectName());
7880
}
7981

82+
public function testGetVerbosity(): void
83+
{
84+
$this->assertSame(OutputInterface::VERBOSITY_NORMAL, $this->setup->getVerbosity());
85+
}
86+
8087
public function testGetBuild(): void
8188
{
8289
/** @var SymfonyStyle & MockInterface $console */

tests/LibraryStarterKit/Task/Builder/UpdateSourceFileHeadersTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Ramsey\Test\Dev\LibraryStarterKit\SnapshotsTool;
1616
use Ramsey\Test\Dev\LibraryStarterKit\TestCase;
1717
use Ramsey\Test\Dev\LibraryStarterKit\WindowsSafeTextDriver;
18+
use Symfony\Component\Console\Output\OutputInterface;
1819
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Symfony\Component\Finder\Finder;
2021
use Symfony\Component\Finder\SplFileInfo;
@@ -46,6 +47,7 @@ public function testBuild(string $license, ?string $copyrightEmail = null, ?stri
4647
$this->mockery(Event::class),
4748
$this->mockery(Filesystem::class),
4849
$this->mockery(Finder::class),
50+
OutputInterface::VERBOSITY_NORMAL,
4951
);
5052

5153
$file1 = $this->mockery(SplFileInfo::class, [

0 commit comments

Comments
 (0)