Skip to content

Commit 333b776

Browse files
committed
Refactor all command class exception handling
1 parent f68f060 commit 333b776

File tree

10 files changed

+663
-615
lines changed

10 files changed

+663
-615
lines changed

src/SPC/builder/BuilderBase.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
namespace SPC\builder;
66

7-
use PharIo\FileSystem\File;
8-
use SPC\exception\ExceptionHandler;
9-
use SPC\exception\FileSystemException;
7+
use SPC\exception\BuildFailureException;
108
use SPC\exception\InterruptException;
11-
use SPC\exception\RuntimeException;
129
use SPC\exception\WrongUsageException;
1310
use SPC\store\Config;
1411
use SPC\store\FileSystem;
1512
use SPC\store\LockFile;
1613
use SPC\store\SourceManager;
1714
use SPC\store\SourcePatcher;
18-
use SPC\util\CustomExt;
15+
use SPC\util\AttributeMapper;
1916

2017
abstract class BuilderBase
2118
{
@@ -64,12 +61,11 @@ public function setupLibs(): void
6461
match ($status) {
6562
LIB_STATUS_OK => logger()->info('lib [' . $lib::NAME . '] setup success, took ' . round(microtime(true) - $starttime, 2) . ' s'),
6663
LIB_STATUS_ALREADY => logger()->notice('lib [' . $lib::NAME . '] already built'),
67-
LIB_STATUS_BUILD_FAILED => logger()->error('lib [' . $lib::NAME . '] build failed'),
6864
LIB_STATUS_INSTALL_FAILED => logger()->error('lib [' . $lib::NAME . '] install failed'),
6965
default => logger()->warning('lib [' . $lib::NAME . '] build status unknown'),
7066
};
7167
if (in_array($status, [LIB_STATUS_BUILD_FAILED, LIB_STATUS_INSTALL_FAILED])) {
72-
throw new RuntimeException('Library [' . $lib::NAME . '] setup failed.');
68+
throw new BuildFailureException('Library [' . $lib::NAME . '] setup failed.');
7369
}
7470
}
7571
}
@@ -254,9 +250,8 @@ public function buildSharedExts(): void
254250
}
255251
$ext->buildShared();
256252
}
257-
} catch (RuntimeException $e) {
253+
} finally {
258254
FileSystem::replaceFileLineContainsString(BUILD_BIN_PATH . '/php-config', 'extension_dir=', $extension_dir_line);
259-
throw $e;
260255
}
261256
FileSystem::replaceFileLineContainsString(BUILD_BIN_PATH . '/php-config', 'extension_dir=', $extension_dir_line);
262257
FileSystem::replaceFileStr(BUILD_LIB_PATH . '/php/build/phpize.m4', '# test "[$]$1" = "no" && $1=yes', 'test "[$]$1" = "no" && $1=yes');
@@ -311,7 +306,7 @@ public function getPHPVersionID(): int
311306
return intval($match[1]);
312307
}
313308

314-
throw new RuntimeException('PHP version file format is malformed, please remove it and download again');
309+
throw new WrongUsageException('PHP version file format is malformed, please remove "./source/php-src" dir and download/extract again');
315310
}
316311

317312
public function getPHPVersion(bool $exception_on_failure = true): string
@@ -329,7 +324,7 @@ public function getPHPVersion(bool $exception_on_failure = true): string
329324
if (!$exception_on_failure) {
330325
return 'unknown';
331326
}
332-
throw new RuntimeException('PHP version file format is malformed, please remove it and download again');
327+
throw new WrongUsageException('PHP version file format is malformed, please remove it and download again');
333328
}
334329

335330
/**
@@ -476,7 +471,7 @@ public function emitPatchPoint(string $point_name): void
476471
foreach ($patches as $patch) {
477472
try {
478473
if (!file_exists($patch)) {
479-
throw new RuntimeException("Additional patch script file {$patch} not found!");
474+
throw new WrongUsageException("Additional patch script file {$patch} not found!");
480475
}
481476
logger()->debug('Running additional patch script: ' . $patch);
482477
require $patch;
@@ -489,11 +484,6 @@ public function emitPatchPoint(string $point_name): void
489484
exit($e->getCode());
490485
} catch (\Throwable $e) {
491486
logger()->critical('Patch script ' . $patch . ' failed to run.');
492-
if ($this->getOption('debug')) {
493-
ExceptionHandler::getInstance()->handle($e);
494-
} else {
495-
logger()->critical('Please check with --debug option to see more details.');
496-
}
497487
throw $e;
498488
}
499489
}

src/SPC/command/BaseCommand.php

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Psr\Log\LogLevel;
1010
use SPC\ConsoleApplication;
1111
use SPC\exception\ExceptionHandler;
12-
use SPC\exception\ValidationException;
13-
use SPC\exception\WrongUsageException;
12+
use SPC\exception\SPCException;
13+
use SPC\util\AttributeMapper;
1414
use SPC\util\GlobalEnvManager;
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -32,6 +32,7 @@ public function __construct(?string $name = null)
3232
parent::__construct($name);
3333
$this->addOption('debug', null, null, 'Enable debug mode');
3434
$this->addOption('no-motd', null, null, 'Disable motd');
35+
$this->addOption('preserve-log', null, null, 'Preserve log files, do not delete them on initialized');
3536
}
3637

3738
public function initialize(InputInterface $input, OutputInterface $output): void
@@ -93,30 +94,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9394
GlobalEnvManager::init();
9495
f_putenv('SPC_SKIP_TOOLCHAIN_CHECK=yes');
9596
}
96-
if ($this->shouldExecute()) {
97-
try {
98-
// show raw argv list for logger()->debug
99-
logger()->debug('argv: ' . implode(' ', $_SERVER['argv']));
100-
return $this->handle();
101-
} catch (ValidationException|WrongUsageException $e) {
102-
$msg = explode("\n", $e->getMessage());
103-
foreach ($msg as $v) {
104-
logger()->error($v);
105-
}
106-
return static::FAILURE;
107-
} catch (\Throwable $e) {
108-
if ($this->getOption('debug')) {
109-
ExceptionHandler::getInstance()->handle($e);
110-
} else {
111-
$msg = explode("\n", $e->getMessage());
112-
foreach ($msg as $v) {
113-
logger()->error($v);
114-
}
115-
}
116-
return static::FAILURE;
117-
}
97+
98+
try {
99+
// show raw argv list for logger()->debug
100+
logger()->debug('argv: ' . implode(' ', $_SERVER['argv']));
101+
return $this->handle();
102+
} /* @noinspection PhpRedundantCatchClauseInspection */ catch (SPCException $e) {
103+
// Handle SPCException and log it
104+
ExceptionHandler::handleSPCException($e);
105+
return static::FAILURE;
106+
} catch (\Throwable $e) {
107+
// Handle any other exceptions
108+
ExceptionHandler::handleDefaultException($e);
109+
return static::FAILURE;
118110
}
119-
return static::SUCCESS;
120111
}
121112

122113
protected function getOption(string $name): mixed
@@ -129,11 +120,6 @@ protected function getArgument(string $name): mixed
129120
return $this->input->getArgument($name);
130121
}
131122

132-
protected function shouldExecute(): bool
133-
{
134-
return true;
135-
}
136-
137123
protected function logWithResult(bool $result, string $success_msg, string $fail_msg): int
138124
{
139125
if ($result) {

src/SPC/command/BuildLibsCommand.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,22 @@ public function handle(): int
5050
}
5151
}
5252

53-
try {
54-
// 构建对象
55-
$builder = BuilderProvider::makeBuilderByInput($this->input);
56-
// 只编译 library 的情况下,标记
57-
$builder->setLibsOnly();
58-
// 编译和检查库完整
59-
$libraries = DependencyUtil::getLibs($libraries);
60-
$display_libs = array_filter($libraries, fn ($lib) => in_array(Config::getLib($lib, 'type', 'lib'), ['lib', 'package']));
53+
// 构建对象
54+
$builder = BuilderProvider::makeBuilderByInput($this->input);
55+
// 只编译 library 的情况下,标记
56+
$builder->setLibsOnly();
57+
// 编译和检查库完整
58+
$libraries = DependencyUtil::getLibs($libraries);
59+
$display_libs = array_filter($libraries, fn ($lib) => in_array(Config::getLib($lib, 'type', 'lib'), ['lib', 'package']));
6160

62-
logger()->info('Building libraries: ' . implode(',', $display_libs));
63-
sleep(2);
64-
$builder->proveLibs($libraries);
65-
$builder->validateLibsAndExts();
66-
$builder->setupLibs();
61+
logger()->info('Building libraries: ' . implode(',', $display_libs));
62+
sleep(2);
63+
$builder->proveLibs($libraries);
64+
$builder->validateLibsAndExts();
65+
$builder->setupLibs();
6766

68-
$time = round(microtime(true) - START_TIME, 3);
69-
logger()->info('Build libs complete, used ' . $time . ' s !');
70-
return static::SUCCESS;
71-
} catch (\Throwable $e) {
72-
if ($this->getOption('debug')) {
73-
ExceptionHandler::getInstance()->handle($e);
74-
} else {
75-
logger()->critical('Build failed with ' . get_class($e) . ': ' . $e->getMessage());
76-
logger()->critical('Please check with --debug option to see more details.');
77-
}
78-
return static::FAILURE;
79-
}
67+
$time = round(microtime(true) - START_TIME, 3);
68+
logger()->info('Build libs complete, used ' . $time . ' s !');
69+
return static::SUCCESS;
8070
}
8171
}

0 commit comments

Comments
 (0)