Merge pull request #1280 from jooola/enhance/api_client
Better error handling in api_client and airtime_mvc
This commit is contained in:
commit
579dfe5b9e
2 changed files with 38 additions and 5 deletions
|
@ -1427,7 +1427,16 @@ class ApiController extends Zend_Controller_Action
|
||||||
|
|
||||||
public function pushStreamStatsAction() {
|
public function pushStreamStatsAction() {
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$data = json_decode($request->getParam("data"), true);
|
|
||||||
|
$data_blob = $request->getParam("data");
|
||||||
|
$data = json_decode($data_blob, true);
|
||||||
|
|
||||||
|
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
$message = "An error occured while decoding the 'data' JSON blob: '$data_blob'";
|
||||||
|
Logging::error($message);
|
||||||
|
$this->jsonError(400, $message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Application_Model_ListenerStat::insertDataPoints($data);
|
Application_Model_ListenerStat::insertDataPoints($data);
|
||||||
$this->view->data = $data;
|
$this->view->data = $data;
|
||||||
|
@ -1435,7 +1444,16 @@ class ApiController extends Zend_Controller_Action
|
||||||
|
|
||||||
public function updateStreamSettingTableAction() {
|
public function updateStreamSettingTableAction() {
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$data = json_decode($request->getParam("data"), true);
|
|
||||||
|
$data_blob = $request->getParam("data");
|
||||||
|
$data = json_decode($data_blob, true);
|
||||||
|
|
||||||
|
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
$message = "An error occured while decoding the 'data' JSON blob: '$data_blob'";
|
||||||
|
Logging::error($message);
|
||||||
|
$this->jsonError(400, $message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($data as $k=>$v) {
|
foreach ($data as $k=>$v) {
|
||||||
Application_Model_StreamSetting::SetListenerStatError($k, $v);
|
Application_Model_StreamSetting::SetListenerStatError($k, $v);
|
||||||
|
@ -1711,4 +1729,18 @@ class ApiController extends Zend_Controller_Action
|
||||||
// enable cors access from configured URLs
|
// enable cors access from configured URLs
|
||||||
CORSHelper::enableCrossOriginRequests($request, $response);
|
CORSHelper::enableCrossOriginRequests($request, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Respond with a JSON error message with a custom HTTP status code.
|
||||||
|
*
|
||||||
|
* This logic should be handled by Zend, but I lack understanding of this
|
||||||
|
* framework, and prefer not break it or spend too much time on it.
|
||||||
|
*/
|
||||||
|
private final function jsonError($status, $message)
|
||||||
|
{
|
||||||
|
$this->getResponse()
|
||||||
|
->setHttpResponseCode($status)
|
||||||
|
->setHeader('Content-Type', 'application/json')
|
||||||
|
->setBody(json_encode(['error' => $message]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,7 +95,7 @@ class ApiRequest:
|
||||||
final_url = self.url.params(**kwargs).url()
|
final_url = self.url.params(**kwargs).url()
|
||||||
self.logger.debug(final_url)
|
self.logger.debug(final_url)
|
||||||
try:
|
try:
|
||||||
if _post_data:
|
if _post_data is not None:
|
||||||
res = requests.post(
|
res = requests.post(
|
||||||
final_url,
|
final_url,
|
||||||
data=_post_data,
|
data=_post_data,
|
||||||
|
@ -121,8 +121,9 @@ class ApiRequest:
|
||||||
raise
|
raise
|
||||||
except requests.exceptions.HTTPError:
|
except requests.exceptions.HTTPError:
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
f"HTTP request to '{res.request.url}' failed"
|
f"{res.request.method} {res.request.url} request failed '{res.status_code}':"
|
||||||
f" with status '{res.status_code}':\n{res.text}"
|
f"\nPayload: {res.request.body}"
|
||||||
|
f"\nResponse: {res.text}"
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue