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

Extend RestException to also accept error object #126

Open
wants to merge 1 commit 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
10 changes: 9 additions & 1 deletion source/Jacwright/RestServer/RestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@
use Exception;

class RestException extends Exception {
public function __construct($code, $message = null) {
private $_data = '';

public function __construct($code, $message = null, $data = null) {
$this->_data = $data;
parent::__construct($message, $code);
}

public function getData()
{
return $this->_data;
}
}
6 changes: 3 additions & 3 deletions source/Jacwright/RestServer/RestServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function handle() {
}
}
} catch (RestException $e) {
$this->handleError($e->getCode(), $e->getMessage());
$this->handleError($e->getCode(), $e->getMessage(), $e->getData());
}
} else {
$this->handleError(404);
Expand Down Expand Up @@ -196,7 +196,7 @@ public function addErrorClass($class) {
$this->errorClasses[] = $class;
}

public function handleError($statusCode, $errorMessage = null) {
public function handleError($statusCode, $errorMessage = null, $data = null) {
$method = "handle$statusCode";

foreach ($this->errorClasses as $class) {
Expand All @@ -220,7 +220,7 @@ public function handleError($statusCode, $errorMessage = null) {
}

$this->setStatus($statusCode);
$this->sendData(array('error' => array('code' => $statusCode, 'message' => $errorMessage)));
$this->sendData(array('error' => array('code' => $statusCode, 'message' => $errorMessage, 'data' => $data)));
}

protected function instantiateClass($obj) {
Expand Down