Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Error handling fix: split exception class with suitable message

Currently the library uses Exception class directly with just
two error message for any error.
So user have poor way to handle errors.

This fix split exception class for each case and throw it
with suitable error messasge.
  • Loading branch information...
commit f8f374b12bc51ebc01137c03da7293c99bb6b90b 1 parent 8e6bed4
@sugi sugi authored
View
14 php/lib/Back.php
@@ -3,8 +3,6 @@
class MessagePackRPC_Back
{
- public $errorMessage01 = 'Network error';
- public $errorMessage02 = 'Objects error';
public $size;
public static $shared_client_socket = null;
public $client_socket = null;
@@ -44,11 +42,11 @@ public function clientConnection($host, $port, $call)
$size = $this->size;
$send = $this->msgpackEncode($call);
$sock = $this->connect($host, $port);
- if ($sock === FALSE) throw new Exception($this->errorMessage01);
+ if ($sock === FALSE) throw new MessagePackRPC_Error_NetworkError(error_get_last());
$puts = fputs($sock, $send);
- if ($puts === FALSE) throw new Exception($this->errorMessage01);
+ if ($puts === FALSE) throw new MessagePackRPC_Error_NetworkError(error_get_last());
$read = fread($sock, $size);
- if ($read === FALSE) throw new Exception($this->errorMessage01);
+ if ($read === FALSE) throw new MessagePackRPC_Error_NetworkError(error_get_last());
if (!$this->reuse_connection)
fclose($sock);
@@ -84,7 +82,7 @@ public function clientRecvObject($recv)
$sets = $data[3];
if ($type != 1) {
- throw new Exception($this->errorMessage02);
+ throw new MessagePackRPC_Error_ProtocolError("Invalid message type for response: {$type}");
}
$feature = new MessagePackRPC_Future();
@@ -112,7 +110,7 @@ public function serverRecvObject($recv)
$data = $this->msgpackDecode($recv);
if (count($data) != 4) {
- throw new Exception($this->errorMessage02);
+ throw new MessagePackRPC_Error_ProtocolError("Invalid message structure.");
}
$type = $data[0];
@@ -121,7 +119,7 @@ public function serverRecvObject($recv)
$args = $data[3];
if ($type != 0) {
- throw new Exception($this->errorMessage02);
+ throw new MessagePackRPC_Error_ProtocolError("Invalid message type for request: {$type}");
}
return array($code, $func, $args);
View
2  php/lib/Client.php
@@ -36,7 +36,7 @@ public function send($func, $args)
$errors = print_r($errors, true);
}
}
- throw new Exception("{$errors}");
+ throw new MessagePackRPC_Error_RequestError("{$errors}");
}
return $result;
View
15 php/lib/Error.php
@@ -0,0 +1,15 @@
+<?php
+
+class MessagePackRPC_Error extends Exception
+{
+ function __construct($msg = '', $code = null, $prev = null) {
+ if (is_array($msg) && array_key_exists('message', $msg))
+ $msg = $msg['message'];
+ parent::__construct($msg, $code, $prev);
+ }
+}
+class MessagePackRPC_Error_NetworkError extends MessagePackRPC_Error {}
+class MessagePackRPC_Error_ProtocolError extends MessagePackRPC_Error {}
+class MessagePackRPC_Error_RequestError extends MessagePackRPC_Error {}
+
+
View
4 php/lib/Future.php
@@ -1,4 +1,6 @@
<?php
+require_once dirname(__FILE__) . '/Error.php';
+
class MessagePackRPC_Future
{
public $result = null;
@@ -29,4 +31,4 @@ public function getErrors()
$errors = $this->errors;
return $errors;
}
-}
+}
View
2  php/lib/Server.php
@@ -33,7 +33,7 @@ public function recv()
$sockList = array($this->_listen_socket);
if ($this->_listen_socket === FALSE) {
- throw new Exception(); // TODO:
+ throw new MessagePackRPC_Error_NetworkError(error_get_last());
}
// TODO : Server connection check
View
2  php/test/client.php
@@ -20,7 +20,7 @@ function testIs($no, $a, $b)
testIs('test0001', 5, $client->call('hello2', array(3)));
try {
$client->call('fail', array());
- } catch (Exception $e) {
+ } catch (MessagePackRPC_Error_RequestError $e) {
echo "OK (proper error)\n";
}
} catch (Exception $e) {
Please sign in to comment.
Something went wrong with that request. Please try again.