Platformsh\Cli\Console\EventSubscriber::onException PHP Method

onException() public method

React to any console exceptions.
public onException ( Symfony\Component\Console\Event\ConsoleExceptionEvent $event )
$event Symfony\Component\Console\Event\ConsoleExceptionEvent
    public function onException(ConsoleExceptionEvent $event)
    {
        $exception = $event->getException();
        // Replace Guzzle connect exceptions with a friendlier message. This
        // also prevents the user from seeing two exceptions (one direct from
        // Guzzle, one from RingPHP).
        if ($exception instanceof ConnectException && strpos($exception->getMessage(), 'cURL error 6') !== false) {
            $request = $exception->getRequest();
            $event->setException(new ConnectionFailedException("Failed to connect to host: " . $request->getHost() . " \nPlease check your Internet connection.", $request));
            $event->stopPropagation();
        }
        // Handle Guzzle exceptions, i.e. HTTP 4xx or 5xx errors.
        if (($exception instanceof ClientException || $exception instanceof ServerException) && ($response = $exception->getResponse())) {
            $request = $exception->getRequest();
            $response->getBody()->seek(0);
            $json = (array) json_decode($response->getBody()->getContents(), true);
            // Create a friendlier message for the OAuth2 "Invalid refresh token"
            // error.
            $loginCommand = sprintf('%s login', $this->config->get('application.executable'));
            if ($response->getStatusCode() === 400 && isset($json['error_description']) && $json['error_description'] === 'Invalid refresh token') {
                $event->setException(new LoginRequiredException("Invalid refresh token. \nPlease log in again by running: {$loginCommand}", $request, $response));
                $event->stopPropagation();
            } elseif ($response->getStatusCode() === 401) {
                $event->setException(new LoginRequiredException("Unauthorized. \nPlease log in again by running: {$loginCommand}", $request, $response));
                $event->stopPropagation();
            } elseif ($response->getStatusCode() === 403) {
                $event->setException(new PermissionDeniedException("Permission denied. Check your project or environment permissions.", $request, $response));
                $event->stopPropagation();
            } else {
                $event->setException(new HttpException(null, $request, $response));
                $event->stopPropagation();
            }
        }
        // When an environment is found to be in the wrong state, perhaps our
        // cache is old - we should invalidate it.
        if ($exception instanceof EnvironmentStateException) {
            $api = new Api();
            $api->clearEnvironmentsCache($exception->getEnvironment()->project);
        }
    }