API - красивый ответ при ошибке

This commit is contained in:
Krivchikov Dmitry 2016-04-24 01:14:54 +03:00
parent 95ee46483e
commit 3bf414a7ac
1 changed files with 17 additions and 7 deletions

View File

@ -7,7 +7,7 @@ class ApiController extends Controller
{ {
$request = json_decode(file_get_contents('php://input'), 1); $request = json_decode(file_get_contents('php://input'), 1);
if (!is_array($request)) { if (!is_array($request)) {
App::error404(); self::error('Bad request!');
} }
$method = isset($request['method']) ? $request['method'] : NULL; $method = isset($request['method']) ? $request['method'] : NULL;
@ -28,7 +28,7 @@ class ApiController extends Controller
$answer = self::getPoint($data); $answer = self::getPoint($data);
break; break;
default: default:
App::error404(); self::error('Unknown method!');
break; break;
} }
@ -58,7 +58,7 @@ class ApiController extends Controller
$result[] = $tempPoint; $result[] = $tempPoint;
} }
return array('data' => $result); return array('status' => 'success','data' => $result);
} }
/** /**
@ -69,13 +69,13 @@ class ApiController extends Controller
$id = isset($data['id']) ? intval($data['id']) : NULL; $id = isset($data['id']) ? intval($data['id']) : NULL;
if (!$id) { if (!$id) {
return FALSE; self::error('Broken request!');
} }
$point = Point::model()->getByPK($id); $point = Point::model()->getByPK($id);
if (!$point) { if (!$point) {
return FALSE; self::error('Unknown point!');
} }
$point->descriptionHtml = nl2br($point->description); $point->descriptionHtml = nl2br($point->description);
@ -116,7 +116,7 @@ class ApiController extends Controller
$point->inFavorites = Favorite::model()->checkIsMy($id); $point->inFavorites = Favorite::model()->checkIsMy($id);
$point->iLike = Like::model()->checkIsMy($id); $point->iLike = Like::model()->checkIsMy($id);
return array('data' => $point->getValues()); return array('status' => 'success','data' => $point->getValues());
} }
/** /**
@ -135,7 +135,17 @@ class ApiController extends Controller
$categories = array_reverse($categories); $categories = array_reverse($categories);
return array('data' => $categories); return array('status' => 'success','data' => $categories);
} }
static private function error($message = FALSE)
{
$answer = array('status' => 'error');
if ($message) {
$answer['message'] = $message;
}
self::renderPartial('json.php', $answer);
}
} }