diff --git a/Cli/EventListener/ApiErrorListener.php b/Cli/EventListener/ApiErrorListener.php new file mode 100644 index 0000000..7aab025 --- /dev/null +++ b/Cli/EventListener/ApiErrorListener.php @@ -0,0 +1,59 @@ + ['onConsoleError', 256], + ]; + } + + public function onConsoleError(ConsoleErrorEvent $event): void + { + $output = $event->getOutput(); + $error = $event->getError(); + + if ($error instanceof ApiClientException) { + $this->showClientError($error, $output); + } elseif ($error instanceof ApiServerException) { + $output->writeln('Server temporarily unavailable. Please try again in a few minutes.'); + } elseif ($error instanceof ApiParserException) { + $output->writeln('Unable to process server response. Please try again later.'); + } elseif ($error instanceof TransportExceptionInterface) { + $output->writeln('Network connection failed. Check your internet connection.'); + } else { + $output->writeln('Something went wrong. Please try again, or run with -v for more details.'); + } + + if ($output->isVerbose()) { + throw $error; + } + + $event->setExitCode(0); + } + + private function showClientError(ApiClientException $e, $output): void + { + $message = $e->getMessage(); + if (false !== strpos($message, '401') || false !== strpos($message, 'Unauthorized')) { + $output->writeln('Invalid API credentials. Run "php insight.phar configure" to set up your token.'); + } elseif (false !== strpos($message, '404') || false !== strpos($message, 'Not Found')) { + $output->writeln('Project not found. Check the project UUID and try again.'); + } elseif (false !== strpos($message, '403') || false !== strpos($message, 'Forbidden')) { + $output->writeln('Access denied. You don\'t have permission for this project.'); + } else { + $output->writeln('Request failed. Please check your input and try again.'); + } + } +} diff --git a/Sdk/Api.php b/Sdk/Api.php index d8a5243..12edc06 100644 --- a/Sdk/Api.php +++ b/Sdk/Api.php @@ -289,12 +289,22 @@ private function processClientError(HttpExceptionInterface $e) private function logException(ExceptionInterface $e) { - $message = sprintf("Exception: Class: \"%s\", Message: \"%s\", Response:\n%s", - \get_class($e), - $e->getMessage(), - $e->getResponse()->getInfo('debug') - ); + if (!$this->logger) { + return; + } + + $parts = [ + sprintf('Exception: Class: "%s"', \get_class($e)), + sprintf('Message: "%s"', $e->getMessage()), + ]; + + if ($e instanceof HttpExceptionInterface) { + $debug = $e->getResponse()->getInfo('debug'); + if ($debug) { + $parts[] = "Response:\n".$debug; + } + } - $this->logger && $this->logger->error($message, ['exception' => $e]); + $this->logger->error(implode(', ', $parts), ['exception' => $e]); } } diff --git a/bin/insight b/bin/insight index 030332d..b5f3bcd 100755 --- a/bin/insight +++ b/bin/insight @@ -18,7 +18,11 @@ if (file_exists($a = __DIR__.'/../../../autoload.php')) { } use SensioLabs\Insight\Cli\Application; +use Symfony\Component\EventDispatcher\EventDispatcher; +use SensioLabs\Insight\Cli\EventListener\ApiErrorListener; $application = new Application(); - +$dispatcher = new EventDispatcher(); +$dispatcher->addSubscriber(new ApiErrorListener()); +$application->setDispatcher($dispatcher); $application->run(); diff --git a/composer.json b/composer.json index 317d3ab..f00c997 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "psr/log": "^1.0", "symfony/http-client": "^5.4|^6.4|^7.0", "symfony/console": "^5.4|^6.4|^7.0", - "symfony/expression-language": "^5.4|^6.4|^7.0" + "symfony/expression-language": "^5.4|^6.4|^7.0", + "symfony/event-dispatcher": "^5.4|^6.4|^7.0" }, "require-dev": { "monolog/monolog": "^1.4",