diff --git a/lib/Clients/Service/Localise.php b/lib/Clients/Service/Localise.php index 9b819fe..dc8dcd4 100644 --- a/lib/Clients/Service/Localise.php +++ b/lib/Clients/Service/Localise.php @@ -3,6 +3,7 @@ namespace TwentySixB\Translations\Clients\Service; use Exception; +use GuzzleHttp\Exception\GuzzleException; use TwentySixB\Translations\Exceptions\AuthorizationFailed; use TwentySixB\Translations\LockHandler; @@ -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.' ); @@ -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] ); @@ -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 ); } diff --git a/lib/Translations/ServiceBase.php b/lib/Translations/ServiceBase.php index f49e3f8..3c566f8 100644 --- a/lib/Translations/ServiceBase.php +++ b/lib/Translations/ServiceBase.php @@ -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(), + ] ); } } diff --git a/lib/Translations/Upload.php b/lib/Translations/Upload.php index 41a2d2a..3d455d9 100644 --- a/lib/Translations/Upload.php +++ b/lib/Translations/Upload.php @@ -22,6 +22,7 @@ class Upload extends ServiceBase { * @var array */ const ACCEPTED_IMPORT_KEYS = [ + '__project_name', // The project from the config. 'locale', 'ext', 'data', @@ -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 ) {