Skip to content

Commit

Permalink
Add better error output handling and correct some copy-pasta and verb…
Browse files Browse the repository at this point in the history
…iage-related errors.
  • Loading branch information
mlteal authored and baohx2000 committed Apr 6, 2020
1 parent 95d9f14 commit 59b05a6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ atomic help auth:login
## Help!

### What is my Account ID?
1. You can get your account ID by logging into https://atomic-beta.pagely.com and
1. You can get your account ID by logging into https://atomic.pagely.com and
looking at the address in your browser. Your account ID will be the number directly following `/account/` in the address.

2. If you are a collaborator and need the ID for another account, log in to atomic-beta (link above) and use the account switcher
2. If you are a collaborator and need the ID for another account, log in to Atomic (link above) and use the account switcher
(click your name in the upper right) and switch to the account in question. The address in your browser
will change to reflect the account ID you are now looking at.
51 changes: 51 additions & 0 deletions src/Command/ApiErrorOutputTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Pagely\AtomicClient\Command;

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use Pagely\AtomicClient\API\AuthApi;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Pagely\AtomicClient\OauthToken;
use Symfony\Component\Console\Style\SymfonyStyle;

trait ApiErrorOutputTrait
{
public function getFormattedErrorMessages(InputInterface $input, OutputInterface $output, \Throwable $e): void
{

$io = new SymfonyStyle($input, $output);


if (is_subclass_of($e, RequestException::class)) {
$response = $e->getResponse();
$write[] = sprintf(
'%s %s',
$response->getStatusCode(),
$response->getReasonPhrase()
);
/** @var StreamInterface $msg */
$contents = $response->getBody()->getContents();
$msg = json_decode($contents, true);

if (!empty($msg['body']) && is_array($msg['body'])) {
foreach ($msg['body'] as $name => $errorContent) {
$write[] = "`$name`: " . implode(', ', $errorContent['messages']);
}
} elseif (!empty($msg['message'])) {
// in case json decode returns an empty or mangled value
$write[] = $msg['message'];
} else {
$write[] = $e->getMessage();
}
} else {
$write[] = "Error code {$e->getCode()} occurred.";
$write[] = $e->getMessage();
}

$io->error($write);
}
}
28 changes: 18 additions & 10 deletions src/Command/Apps/GitIntegration/CreateGitIntegrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

namespace Pagely\AtomicClient\Command\Apps\GitIntegration;

use Pagely\Client\AuthApi;
use Pagely\Client\Command\Command;
use Pagely\Client\Command\OauthCommandTrait;
use Pagely\Client\AppsApi\AppsClient;
use Pagely\AtomicClient\API\AuthApi;
use Pagely\AtomicClient\Command\ApiErrorOutputTrait;
use Pagely\AtomicClient\Command\Command;
use Pagely\AtomicClient\Command\OauthCommandTrait;
use Pagely\AtomicClient\API\Apps\AppsClient;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CreateGitIntegrationCommand extends Command
{
use OauthCommandTrait;
use ApiErrorOutputTrait;

/**
* @var AppsClient
Expand Down Expand Up @@ -43,12 +45,18 @@ public function execute(InputInterface $input, OutputInterface $output)
$token = $this->token->token;
$output->writeln('Create integration initiated.');

$this->api->createGitIntegration(
$token,
(int) $input->getArgument('appId'),
strtolower($input->getArgument('remote')),
$input->getArgument('branch')
);
try {
$r = $this->api->createGitIntegration(
$token,
(int) $input->getArgument('appId'),
strtolower($input->getArgument('remote')),
$input->getArgument('branch')
);
} catch (\Throwable $e) {
$this->getFormattedErrorMessages($input, $output, $e);
return 1;
}


$output->writeln('Create integration executed.');
return 0;
Expand Down
23 changes: 15 additions & 8 deletions src/Command/Apps/GitIntegration/DeleteGitIntegrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

namespace Pagely\AtomicClient\Command\Apps\GitIntegration;

use Pagely\Client\AuthApi;
use Pagely\Client\Command\Command;
use Pagely\Client\Command\OauthCommandTrait;
use Pagely\Client\AppsApi\AppsClient;
use Pagely\AtomicClient\API\AuthApi;
use Pagely\AtomicClient\Command\ApiErrorOutputTrait;
use Pagely\AtomicClient\Command\Command;
use Pagely\AtomicClient\Command\OauthCommandTrait;
use Pagely\AtomicClient\API\Apps\AppsClient;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DeleteGitIntegrationCommand extends Command
{
use OauthCommandTrait;
use ApiErrorOutputTrait;

/**
* @var AppsClient
Expand Down Expand Up @@ -40,10 +42,15 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$token = $this->token->token;

$this->api->deleteGitIntegration(
$token,
(int) $input->getArgument('appId')
);
try {
$this->api->deleteGitIntegration(
$token,
(int)$input->getArgument('appId')
);
} catch (\Throwable $e) {
$this->getFormattedErrorMessages($input, $output, $e);
return 1;
}

$output->writeln('Delete integration executed.');
return 0;
Expand Down
23 changes: 15 additions & 8 deletions src/Command/Apps/GitIntegration/GetGitIntegrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Pagely\AtomicClient\Command\Apps\GitIntegration;

use Pagely\Client\AuthApi;
use Pagely\Client\Command\Command;
use Pagely\Client\Command\OauthCommandTrait;
use Pagely\Client\AppsApi\AppsClient;
use Pagely\AtomicClient\API\AuthApi;
use Pagely\AtomicClient\Command\ApiErrorOutputTrait;
use Pagely\AtomicClient\Command\Command;
use Pagely\AtomicClient\Command\OauthCommandTrait;
use Pagely\AtomicClient\API\Apps\AppsClient;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -14,6 +15,7 @@
class GetGitIntegrationCommand extends Command
{
use OauthCommandTrait;
use ApiErrorOutputTrait;

/**
* @var AppsClient
Expand Down Expand Up @@ -41,10 +43,15 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$token = $this->token->token;

$r = $this->api->getGitIntegration(
$token,
(int) $input->getArgument('appId')
);
try {
$r = $this->api->getGitIntegration(
$token,
(int)$input->getArgument('appId')
);
} catch (\Throwable $e) {
$this->getFormattedErrorMessages($input, $output, $e);
return 1;
}

$body = json_decode($r->getBody()->getContents(), true);

Expand Down

0 comments on commit 59b05a6

Please sign in to comment.