send http status code accordingly to status

master
Gilles Crettenand 11 years ago
parent ab2092e9c3
commit 02c2ebfa7f

@ -7,6 +7,7 @@ use BSR\Lib\Exception\WebException;
abstract class WebService abstract class WebService
{ {
private $func = null; private $func = null;
private $status = 200;
private $log = ''; private $log = '';
@ -40,10 +41,14 @@ abstract class WebService
$data["error"]["code"] = $e->getCode(); $data["error"]["code"] = $e->getCode();
$data["error"]["name"] = $e->getName(); $data["error"]["name"] = $e->getName();
$data["error"]["reason"] = $e->getMessage(); $data["error"]["reason"] = $e->getMessage();
$this->log(sprintf("Failure : [%s] %s", $e->getCode(), $e->getName())); $this->status = 400;
$this->log(sprintf("Error : [%s] %s", $e->getCode(), $e->getName()));
} catch (\Exception $e) { } catch (\Exception $e) {
$data["unexpected error"]["message"] = $e->getMessage(); $data["failure"]["message"] = $e->getMessage();
$this->log(sprintf("Unexpected exception : %s", $e->getMessage())); $this->status = 500;
$this->log(sprintf("Failure : %s", $e->getMessage()));
} }
$this->Send($data); $this->Send($data);
@ -98,6 +103,16 @@ abstract class WebService
private function Send(array $data) private function Send(array $data)
{ {
static $status_messages = array(
200 => 'Ok',
400 => 'Bad request',
404 => 'Not Found',
403 => 'Not Authorized',
500 => 'Server Error',
);
header(sprintf('HTTP/1.0 %s %s', $this->status, $status_messages[$this->status]));
ob_clean(); ob_clean();
flush(); flush();

Loading…
Cancel
Save