Skip to content

Commit

Permalink
Merge pull request #38 from 26B/feature/exception-visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes authored Dec 7, 2021
2 parents 10c3b23 + 7db81e0 commit 6f405f1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
33 changes: 23 additions & 10 deletions lib/Clients/Service/Localise.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TwentySixB\Translations\Clients\Service;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use TwentySixB\Translations\Exceptions\AuthorizationFailed;
use TwentySixB\Translations\LockHandler;

Expand Down Expand Up @@ -46,11 +47,12 @@ public function authenticate( array $args ) {
]
);

$res = $client->request(
'GET',
'auth/verify',
[]
);
try {
$res = $client->request( 'GET', 'auth/verify', [] );
} catch ( GuzzleException $e ) {
print( "\nException thrown while authenticating for project '{$args['__project_name']}'." );
throw $e;
}

if ( $res->getStatusCode() !== 200 ) {
throw new AuthorizationFailed( 'Authorization was not successful.' );
Expand Down Expand Up @@ -86,7 +88,12 @@ public function export( array $args ) : string {

$url .= empty( $args ) ? '' : '?' . http_build_query( $args );

$res = $this->client->request( 'GET', $url, $this->get_export_options( $last_modified ) );
try {
$res = $this->client->request( 'GET', $url, $this->get_export_options( $last_modified ) );
} catch ( GuzzleException $e ) {
print( "\nException thrown while downloading for project '{$proj_name}'." );
throw $e;
}

LockHandler::get_instance()->set( $proj_name, 'Last-Modified', $res->getHeaders()['Last-Modified'][0] );

Expand All @@ -106,13 +113,19 @@ public function import( array $args ) {
throw new Exception( 'Authenticate should be called first' );
}

$url = sprintf( 'import/%s', $args['ext'] );
$body = $args['data'];
unset( $args['ext'], $args['data'] );
$proj_name = $args['__project_name'];
$url = sprintf( 'import/%s', $args['ext'] );
$body = $args['data'];
unset( $args['__project_name'], $args['ext'], $args['data'], );

$url .= empty( $args ) ? '' : '?' . http_build_query( $args );

$res = $this->client->request( 'POST', $url, [ 'body' => $body ] );
try {
$res = $this->client->request( 'POST', $url, [ 'body' => $body ] );
} catch ( GuzzleException $e ) {
print( "\nException thrown while uploading for project '{$proj_name}'." );
throw $e;
}

return json_decode( $res->getBody()->__toString(), true );
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Translations/ServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public function __construct( Project $config ) {
protected function authenticate() {
$client = $this->config->get_client();
return $client->authenticate(
[ 'key' => $this->config->get_api_key( $client->get_api_key_prefix() ) ]
[
'key' => $this->config->get_api_key( $client->get_api_key_prefix() ),
'__project_name' => $this->config->get_name(),
]
);
}
}
8 changes: 5 additions & 3 deletions lib/Translations/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Upload extends ServiceBase {
* @var array
*/
const ACCEPTED_IMPORT_KEYS = [
'__project_name', // The project from the config.
'locale',
'ext',
'data',
Expand Down Expand Up @@ -72,9 +73,10 @@ public function upload() : void {
* @return array
*/
private function make_import_config( string $locale, string $data ) : array {
$config = $this->config->get_config();
$config['locale'] = $locale;
$config['data'] = $data;
$config = $this->config->get_config();
$config['__project_name'] = $this->config->get_name();
$config['locale'] = $locale;
$config['data'] = $data;
return array_filter(
$config,
function ( $key ) {
Expand Down

0 comments on commit 6f405f1

Please sign in to comment.