Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions src/RocketChatChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand Down
23 changes: 14 additions & 9 deletions src/RocketChatClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -36,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");
}

/**
Expand All @@ -54,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");
}
}

Expand All @@ -72,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");
}
}

Expand All @@ -90,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");
}
}

Expand Down
34 changes: 12 additions & 22 deletions src/RocketChatGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

/**
Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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 ) {
Expand All @@ -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");
}
}

Expand All @@ -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");
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/RocketChatSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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){
Expand Down
13 changes: 5 additions & 8 deletions src/RocketChatUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use RocketChat\Client;

class User extends Client {

public $username;
private $password;
public $id;
Expand Down Expand Up @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}
}