Skip to content

Commit

Permalink
Refactored HTTPExceptions to use Response classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Moore committed Jun 10, 2013
1 parent 3f2389b commit 07a0951
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
34 changes: 20 additions & 14 deletions exceptions/HTTPException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@ public function send(){

$res = $di->get('response');
$req = $di->get('request');
//query string, filter, default

//query string, filter, default
if(!$req->get('suppress_response_codes', null, null)){
$res->setStatusCode($this->getCode(), $this->response)->sendHeaders();
} else {
$res->setStatusCode('200', 'OK')->sendHeaders();
}
$res->setJsonContent(array(
'_meta' => array(
'status' => 'ERROR'
),
'error' => array(
'error' => $this->getCode(),
'userMessage' => $this->getMessage(),
'devMessage' => $this->devMessage,
'more' => $this->additionalInfo,
'errorCode' => $this->errorCode,
)
));
$res->send();

$error = array(
'errorCode' => $this->getCode(),
'userMessage' => $this->getMessage(),
'devMessage' => $this->devMessage,
'more' => $this->additionalInfo,
'applicationCode' => $this->errorCode,
);

if(!$req->get('type') || $req->get('type') == 'json'){
$response = new \PhalconRest\Responses\JSONResponse();
$response->send($error, true);
return;
} else if($req->get('type') == 'csv'){
$response = new \PhalconRest\Responses\CSVResponse();
$response->send(array($error));
return;
}

error_log(
'HTTPException: ' . $this->getFile() .
Expand Down
9 changes: 5 additions & 4 deletions responses/JSONResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public function __construct(){
parent::__construct();
}

public function send($records){
public function send($records, $error=false){

$response = $this->di->get('response');
$success = ($error) ? 'ERROR' : 'SUCCESS';

// If the query string 'envelope' is set to false, do not use the envelope.
// Instead, return headers.
Expand All @@ -30,13 +31,13 @@ public function send($records){
// Provide an envelope for JSON responses. '_meta' and 'records' are the objects.
$message = array();
$message['_meta'] = array(
'status' => 'SUCCESS',
'count' => count($records)
'status' => $success,
'count' => ($error) ? 1 : count($records)
);
$message['records'] = $records;
} else {
$response->setHeader('X-Record-Count', count($records));
$response->setHeader('X-Status', 'SUCCESS');
$response->setHeader('X-Status', $success);
$message = $records;
}

Expand Down

0 comments on commit 07a0951

Please sign in to comment.