-
-
Notifications
You must be signed in to change notification settings - Fork 36
Event approach: clean up API error handling with concise user messages #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace SensioLabs\Insight\Cli\EventListener; | ||
|
||
use SensioLabs\Insight\Sdk\Exception\ApiClientException; | ||
use SensioLabs\Insight\Sdk\Exception\ApiParserException; | ||
use SensioLabs\Insight\Sdk\Exception\ApiServerException; | ||
use Symfony\Component\Console\ConsoleEvents; | ||
use Symfony\Component\Console\Event\ConsoleErrorEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; | ||
|
||
class ApiErrorListener implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
ConsoleEvents::ERROR => ['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('<error>Server temporarily unavailable. Please try again in a few minutes.</error>'); | ||
} elseif ($error instanceof ApiParserException) { | ||
$output->writeln('<error>Unable to process server response. Please try again later.</error>'); | ||
} elseif ($error instanceof TransportExceptionInterface) { | ||
$output->writeln('<error>Network connection failed. Check your internet connection.</error>'); | ||
} else { | ||
$output->writeln('<error>Something went wrong. Please try again, or run with -v for more details.</error>'); | ||
} | ||
|
||
if ($output->isVerbose()) { | ||
throw $error; | ||
} | ||
|
||
$event->setExitCode(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will terminate the process gracefully and display only a user-friendly message. No exception details or additional output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correct, but is that what ppl will expect? I'm not sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exit code is 0 ➜ insight git:(handler) ✗ bin/insight analyze 1c92dfd89776
Something went wrong. Please try again, or run with -v for more details. exit code 1 ➜ insight git:(handler) ✗ bin/insight analyze 1c92dfd89776
Something went wrong. Please try again, or run with -v for more details.
In AnalyzeCommand.php line 43:
Testing the exception handling
analyze [--format FORMAT] [--reference REFERENCE] [--branch BRANCH] [--show-ignored-violations] [--poll-period POLL-PERIOD] [--fail-condition FAIL-CONDITION] [--no-wait] [--] <project-uuid> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't make sense. Optimizing the displayed output shouldn't come at the price of breaking exit codes. Can you figure out another way that doesn't break the exit code? |
||
} | ||
|
||
private function showClientError(ApiClientException $e, $output): void | ||
{ | ||
$message = $e->getMessage(); | ||
if (false !== strpos($message, '401') || false !== strpos($message, 'Unauthorized')) { | ||
$output->writeln('<error>Invalid API credentials. Run "php insight.phar configure" to set up your token.</error>'); | ||
} elseif (false !== strpos($message, '404') || false !== strpos($message, 'Not Found')) { | ||
$output->writeln('<error>Project not found. Check the project UUID and try again.</error>'); | ||
} elseif (false !== strpos($message, '403') || false !== strpos($message, 'Forbidden')) { | ||
$output->writeln('<error>Access denied. You don\'t have permission for this project.</error>'); | ||
} else { | ||
$output->writeln('<error>Request failed. Please check your input and try again.</error>'); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.