From cbbbebc35c2bfdbf8ff210f4dda8212b00f9060f Mon Sep 17 00:00:00 2001 From: Fabio Date: Sat, 20 Jan 2018 10:31:28 -0200 Subject: [PATCH 1/2] Add createExceptionFromResponse method --- src/RocketChatClient.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/RocketChatClient.php b/src/RocketChatClient.php index f619ff0..8da92e7 100644 --- a/src/RocketChatClient.php +++ b/src/RocketChatClient.php @@ -18,6 +18,16 @@ function __construct(){ Request::ini( $tmp ); } + protected function createExceptionFromResponse($response, $prefix){ + if(!empty($response->body->error)){ + return new \Exception("$prefix: ".$response->body->error); + } else if(!empty($response->body)){ + return new \Exception("$prefix: ".$response->body); + } else { + return new \Exception("$prefix: unknown error!"); + } + } + /** * Get version information. This simple method requires no authentication. */ From d246aff1d0c0a2d67f38f44856fbed33a411dd5b Mon Sep 17 00:00:00 2001 From: Fabio Date: Mon, 22 Jan 2018 14:41:21 -0200 Subject: [PATCH 2/2] Better error handling --- src/RocketChatChannel.php | 28 +++++++++------------------- src/RocketChatClient.php | 13 ++++--------- src/RocketChatGroup.php | 34 ++++++++++++---------------------- src/RocketChatSettings.php | 7 +++---- src/RocketChatUser.php | 13 +++++-------- 5 files changed, 33 insertions(+), 62 deletions(-) diff --git a/src/RocketChatChannel.php b/src/RocketChatChannel.php index b649098..906b3a5 100644 --- a/src/RocketChatChannel.php +++ b/src/RocketChatChannel.php @@ -51,8 +51,7 @@ public function create(){ $this->id = $response->body->channel->_id; return $response->body->channel; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not create new channel"); } } @@ -66,8 +65,7 @@ public function info() { $this->id = $response->body->channel->_id; return $response->body; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not get channel info"); } } @@ -87,9 +85,7 @@ public function postMessage( $text ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - if( isset($response->body->error) ) echo( $response->body->error . "\n" ); - else if( isset($response->body->message) ) echo( $response->body->message . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not post message to channel $this->name"); } } @@ -104,8 +100,7 @@ public function close(){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not remove channel from the user’s list of channels"); } } @@ -120,8 +115,7 @@ public function delete(){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not delete channel $this->name"); } } @@ -139,8 +133,7 @@ public function kick( $user ){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not kick user $user from channel $this->name"); } } @@ -158,8 +151,7 @@ public function invite( $user ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not invite user $user to channel $this->name"); } } @@ -177,8 +169,7 @@ public function addOwner( $user ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not add user $user as owner of channel $this->name"); } } @@ -196,8 +187,7 @@ public function removeOwner( $user ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not kick user $user from chanel $this->name"); } } diff --git a/src/RocketChatClient.php b/src/RocketChatClient.php index 8da92e7..2a9806c 100644 --- a/src/RocketChatClient.php +++ b/src/RocketChatClient.php @@ -46,10 +46,8 @@ public function me() { if( isset($response->body->success) && $response->body->success == true ) { return $response->body; } - } else { - echo( $response->body->message . "\n" ); - return false; } + throw $this->createExceptionFromResponse($response, "Could not list channels"); } /** @@ -64,8 +62,7 @@ public function list_users(){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return $response->body->users; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not list users"); } } @@ -82,8 +79,7 @@ public function list_groups() { } return $groups; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not list groups"); } } @@ -100,8 +96,7 @@ public function list_channels() { } return $groups; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not list channels"); } } diff --git a/src/RocketChatGroup.php b/src/RocketChatGroup.php index b2af9b7..490cff9 100644 --- a/src/RocketChatGroup.php +++ b/src/RocketChatGroup.php @@ -51,8 +51,7 @@ public function create(){ $this->id = $response->body->group->_id; return $response->body->group; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not create a private group"); } } @@ -66,8 +65,7 @@ public function info() { $this->id = $response->body->group->_id; return $response->body; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not get info about the group"); } } @@ -87,10 +85,8 @@ public function postMessage( $text ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - if( isset($response->body->error) ) echo( $response->body->error . "\n" ); - else if( isset($response->body->message) ) echo( $response->body->message . "\n" ); - return false; - } + throw $this->createExceptionFromResponse($response, "Could not post message"); + } } /** @@ -104,8 +100,7 @@ public function close(){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not remove private group"); } } @@ -120,8 +115,7 @@ public function delete(){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not delete a private group"); } } @@ -139,8 +133,7 @@ public function kick( $user ){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could kick user $user from group"); } } @@ -158,12 +151,11 @@ public function invite( $user ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; - } + throw $this->createExceptionFromResponse($response, "Could not invite user $user to the private group"); + } } - /** + /** * Adds owner to the private group. */ public function addOwner( $user ) { @@ -177,8 +169,7 @@ public function addOwner( $user ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not add user $user as owner of private group"); } } @@ -196,8 +187,7 @@ public function removeOwner( $user ) { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not remove user $user as owner of private group"); } } } diff --git a/src/RocketChatSettings.php b/src/RocketChatSettings.php index cda4a5a..b5702ae 100644 --- a/src/RocketChatSettings.php +++ b/src/RocketChatSettings.php @@ -30,7 +30,7 @@ public function get( $id ){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return $response->body->value; } else { - echo( $response->body->error . "\n" ); + throw $this->createExceptionFromResponse($response, "Could not get setting $id"); } } @@ -45,7 +45,7 @@ public function update( $id, $value ){ if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); + throw $this->createExceptionFromResponse($response, "Could not update setting $id to $value"); } } @@ -59,8 +59,7 @@ public function check(){ fclose($f); if( $settings === null ) { - echo "Erreur de décodage json pour le fichier {$this->file}\n"; - return; + throw new \Exception("Not possible to decode local settings file: $this->file"); } foreach($settings as $id => $value){ diff --git a/src/RocketChatUser.php b/src/RocketChatUser.php index fd21dae..f3b72c3 100644 --- a/src/RocketChatUser.php +++ b/src/RocketChatUser.php @@ -6,6 +6,7 @@ use RocketChat\Client; class User extends Client { + public $username; private $password; public $id; @@ -43,8 +44,7 @@ public function login($save_auth = true) { $this->id = $response->body->data->userId; return true; } else { - echo( $response->body->message . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not authenticate with the REST API"); } } @@ -60,8 +60,7 @@ public function info() { $this->email = $response->body->user->emails[0]->address; return $response->body; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not get user's information"); } } @@ -82,8 +81,7 @@ public function create() { $this->id = $response->body->user->_id; return $response->body->user; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not create new user"); } } @@ -103,8 +101,7 @@ public function delete() { if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { return true; } else { - echo( $response->body->error . "\n" ); - return false; + throw $this->createExceptionFromResponse($response, "Could not delete user"); } } }