Add FreeboxApiException when a service fail

This commit is contained in:
alphayax 2016-06-24 16:18:37 +02:00
parent 7619351a35
commit 6b9f372cd2
4 changed files with 121 additions and 57 deletions

View File

@ -13,7 +13,7 @@
"require" : {
"php": ">=5.5.0",
"ext-curl": "*",
"alphayax/php_utils" : "^1.3.0",
"alphayax/php_utils" : "^1.3.1",
"monolog/monolog": "^1.9.1"
},
"require-dev" : {

View File

@ -0,0 +1,78 @@
<?php
namespace alphayax\freebox\Exception;
/**
* Class FreeboxException
* @package alphayax\freebox\Exception
*/
class FreeboxApiException extends \Exception {
/** @var string */
protected $apiMessage;
/** @var string */
protected $apiErrorCode;
/** @var string */
protected $apiRequest;
/** @var string */
protected $apiHost;
/**
* @return string
*/
public function getApiMessage() {
return $this->apiMessage;
}
/**
* @return string
*/
public function getApiErrorCode() {
return $this->apiErrorCode;
}
/**
* @return string
*/
public function getApiRequest() {
return $this->apiRequest;
}
/**
* @return string
*/
public function getApiHost() {
return $this->apiHost;
}
/**
* @param $message
*/
public function setApiMessage( $message) {
$this->apiMessage = $message;
}
/**
* @param $apiErrorCode
*/
public function setApiErrorCode( $apiErrorCode) {
$this->apiErrorCode = $apiErrorCode;
}
/**
* @param $apiRequest
*/
public function setApiRequest( $apiRequest) {
$this->apiRequest = $apiRequest;
}
/**
* @param $apiHost
*/
public function setApiHost( $apiHost) {
$this->apiHost = $apiHost;
}
}

View File

@ -11,38 +11,38 @@ use alphayax;
class Rest extends alphayax\utils\Rest {
/**
* @param null $curl_post_data
* @param null $curlPostData
* @param bool $checkResponse
* @throws \Exception
*/
public function GET( $curl_post_data = null, $checkResponse = true){
parent::GET( $curl_post_data);
public function GET( $curlPostData = null, $checkResponse = true){
parent::GET( $curlPostData);
if( $checkResponse){
$this->checkResponse();
}
}
/**
* @param $curl_post_data
* @param $curlPostData
*/
public function POST( $curl_post_data = null){
parent::POST( $curl_post_data);
public function POST( $curlPostData = null){
parent::POST( $curlPostData);
$this->checkResponse();
}
/**
* @param $curl_post_data
* @param $curlPostData
*/
public function PUT( $curl_post_data = null){
parent::PUT( $curl_post_data);
public function PUT( $curlPostData = null){
parent::PUT( $curlPostData);
$this->checkResponse();
}
/**
* @param $curl_post_data
* @param $curlPostData
*/
public function DELETE( $curl_post_data = null){
parent::DELETE( $curl_post_data);
public function DELETE( $curlPostData = null){
parent::DELETE( $curlPostData);
$this->checkResponse();
}
@ -50,9 +50,20 @@ class Rest extends alphayax\utils\Rest {
* @throws \Exception
*/
protected function checkResponse(){
$request = explode( "\r\n", $this->curlGetInfo['request_header'])[0];
$url = $this->curlGetInfo['url'];
// echo ">> $request ($url)" . PHP_EOL;
if( false === $this->getSuccess()){
$response = $this->getCurlResponse();
throw new \Exception( $response['msg'] . ' ('. $response['error_code'] . ')');
$response = $this->getCurlResponse();
$Exception = new alphayax\freebox\Exception\FreeboxApiException( $response['msg'] . ' ('. $response['error_code'] . ')');
$Exception->setApiMessage( $response['msg']);
$Exception->setApiErrorCode( $response['error_code']);
$Exception->setApiRequest( $request);
$Exception->setApiHost( $url);
throw $Exception;
}
}

View File

@ -11,79 +11,54 @@ use alphayax;
class RestAuth extends Rest {
/** @var string */
protected $session_token = '';
protected $sessionToken = '';
/**
* @param null $curl_post_data
* @param null $curlPostData
* @param bool $checkResponse
* @throws \Exception
*/
public function GET( $curl_post_data = null, $checkResponse = true){
public function GET( $curlPostData = null, $checkResponse = true){
$this->add_XFbxAppAuth_Header();
parent::GET( $curl_post_data, $checkResponse);
parent::GET( $curlPostData, $checkResponse);
}
/**
* @param $curl_post_data
* @param $curlPostData
*/
public function POST( $curl_post_data = null){
public function POST( $curlPostData = null){
$this->add_XFbxAppAuth_Header();
parent::POST( $curl_post_data);
parent::POST( $curlPostData);
}
/**
* @param $curl_post_data
* @param $curlPostData
*/
public function PUT( $curl_post_data = null){
public function PUT( $curlPostData = null){
$this->add_XFbxAppAuth_Header();
parent::PUT( $curl_post_data);
parent::PUT( $curlPostData);
}
/**
* @param $curl_post_data
* @param $curlPostData
*/
public function DELETE( $curl_post_data = null){
public function DELETE( $curlPostData = null){
$this->add_XFbxAppAuth_Header();
parent::DELETE( $curl_post_data);
parent::DELETE( $curlPostData);
}
/**
* Add the session token in the X-Fbx-App-Auth Header
*/
protected function add_XFbxAppAuth_Header(){
$this->addHeader( 'X-Fbx-App-Auth', $this->session_token);
$this->addHeader( 'X-Fbx-App-Auth', $this->sessionToken);
}
/**
* @param $session_token
* @param $sessionToken
*/
public function setSessionToken( $session_token){
$this->session_token = $session_token;
}
/**
* @throws \Exception
*/
protected function checkResponse(){
$response = $this->getCurlResponse();
$request = explode( "\r\n", $this->curlGetInfo['request_header'])[0] . PHP_EOL;
// echo ">> $request";
if( false === $this->getSuccess()){
/*
switch( $response['error_code']){
case 'invalid_request' :
$a = new alphayax\freebox\Exception\InvalidRequestException();
$a->setHttpRequestHeader( $this->_curl_getinfo['request_header']);
$a->setHttpUrl( $this->_curl_getinfo['url']);
echo PHP_EOL . '---' . PHP_EOL;
echo $this->_curl_getinfo['request_header'];
echo PHP_EOL . '---' . PHP_EOL;
throw $a;
}
*/
throw new \Exception( $request .' - '. $response['msg'] . ' ('. $response['error_code'] . ')');
}
public function setSessionToken( $sessionToken){
$this->sessionToken = $sessionToken;
}
}