Skip to content

Commit

Permalink
Allow exceptions in beforeSend() to error out appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
scottchiefbaker committed Nov 23, 2015
1 parent e252186 commit 7db1b4c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/Server.php
Expand Up @@ -580,16 +580,27 @@ public function var_set(&$value, $default = null) {
public function handle($params = null) {
header("Content-Type: application/json", true);

// Callback time!
$this->onBeforeCall($this);
// Setup error handler
$handler = set_error_handler(array($this, '_errorHandler'), $this->_error_handling_level);

// Prepare current output -> in case of wrong json string
$error = new \stdClass;
$error->jsonrpc = '2.0';
$error->error = new \stdClass;
$error->error->code = -32700;
$error = new \stdClass;
$error->jsonrpc = '2.0';
$error->error = new \stdClass;
$error->error->code = -32700;
$error->error->message = 'Parse error.';
$error->id = null;
$error->id = null;

// Callback time!
try {
$this->onBeforeCall($this);
} catch (\Exception $e) {
// Wrap the exception into request
$error->error->code = $e->getCode();
$error->error->message = get_class($e) . ': ' . $e->getMessage();
$this->onError($this);
return $this->_end($error);
}

// Raw json string?
if(is_string($params)) {
Expand Down Expand Up @@ -662,9 +673,6 @@ public function handle($params = null) {
}
}

// Setup error handler
$handler = set_error_handler(array($this, '_errorHandler'), $this->_error_handling_level);

// ------------------------- Execution time ----------------------------
try {
$output = $this->_handle($input);
Expand Down

0 comments on commit 7db1b4c

Please sign in to comment.