From 677953067f2bb5502a70f0d004f1ac844b18a128 Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 16 Jan 2017 22:04:43 +0100 Subject: * Facebook support --- .../Exceptions/FacebookAuthenticationException.php | 33 ++++ .../Exceptions/FacebookAuthorizationException.php | 33 ++++ .../Exceptions/FacebookClientException.php | 33 ++++ .../Facebook/Exceptions/FacebookOtherException.php | 33 ++++ .../Exceptions/FacebookResponseException.php | 208 +++++++++++++++++++++ .../Facebook/Exceptions/FacebookSDKException.php | 33 ++++ .../Exceptions/FacebookServerException.php | 33 ++++ .../Exceptions/FacebookThrottleException.php | 33 ++++ 8 files changed, 439 insertions(+) create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookAuthenticationException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookAuthorizationException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookClientException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookOtherException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookResponseException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookSDKException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookServerException.php create mode 100644 lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookThrottleException.php (limited to 'lib/facebook-graph-sdk/src/Facebook/Exceptions') diff --git a/lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookAuthenticationException.php b/lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookAuthenticationException.php new file mode 100644 index 0000000..449cf93 --- /dev/null +++ b/lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookAuthenticationException.php @@ -0,0 +1,33 @@ +response = $response; + $this->responseData = $response->getDecodedBody(); + + $errorMessage = $this->get('message', 'Unknown error from Graph.'); + $errorCode = $this->get('code', -1); + + parent::__construct($errorMessage, $errorCode, $previousException); + } + + /** + * A factory for creating the appropriate exception based on the response from Graph. + * + * @param FacebookResponse $response The response that threw the exception. + * + * @return FacebookResponseException + */ + public static function create(FacebookResponse $response) + { + $data = $response->getDecodedBody(); + + if (!isset($data['error']['code']) && isset($data['code'])) { + $data = ['error' => $data]; + } + + $code = isset($data['error']['code']) ? $data['error']['code'] : null; + $message = isset($data['error']['message']) ? $data['error']['message'] : 'Unknown error from Graph.'; + + $previousException = null; + + if (isset($data['error']['error_subcode'])) { + switch ($data['error']['error_subcode']) { + // Other authentication issues + case 458: + case 459: + case 460: + case 463: + case 464: + case 467: + return new static($response, new FacebookAuthenticationException($message, $code)); + } + } + + switch ($code) { + // Login status or token expired, revoked, or invalid + case 100: + case 102: + case 190: + return new static($response, new FacebookAuthenticationException($message, $code)); + + // Server issue, possible downtime + case 1: + case 2: + return new static($response, new FacebookServerException($message, $code)); + + // API Throttling + case 4: + case 17: + case 341: + return new static($response, new FacebookThrottleException($message, $code)); + + // Duplicate Post + case 506: + return new static($response, new FacebookClientException($message, $code)); + } + + // Missing Permissions + if ($code == 10 || ($code >= 200 && $code <= 299)) { + return new static($response, new FacebookAuthorizationException($message, $code)); + } + + // OAuth authentication error + if (isset($data['error']['type']) && $data['error']['type'] === 'OAuthException') { + return new static($response, new FacebookAuthenticationException($message, $code)); + } + + // All others + return new static($response, new FacebookOtherException($message, $code)); + } + + /** + * Checks isset and returns that or a default value. + * + * @param string $key + * @param mixed $default + * + * @return mixed + */ + private function get($key, $default = null) + { + if (isset($this->responseData['error'][$key])) { + return $this->responseData['error'][$key]; + } + + return $default; + } + + /** + * Returns the HTTP status code + * + * @return int + */ + public function getHttpStatusCode() + { + return $this->response->getHttpStatusCode(); + } + + /** + * Returns the sub-error code + * + * @return int + */ + public function getSubErrorCode() + { + return $this->get('error_subcode', -1); + } + + /** + * Returns the error type + * + * @return string + */ + public function getErrorType() + { + return $this->get('type', ''); + } + + /** + * Returns the raw response used to create the exception. + * + * @return string + */ + public function getRawResponse() + { + return $this->response->getBody(); + } + + /** + * Returns the decoded response used to create the exception. + * + * @return array + */ + public function getResponseData() + { + return $this->responseData; + } + + /** + * Returns the response entity used to create the exception. + * + * @return FacebookResponse + */ + public function getResponse() + { + return $this->response; + } +} diff --git a/lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookSDKException.php b/lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookSDKException.php new file mode 100644 index 0000000..03219b0 --- /dev/null +++ b/lib/facebook-graph-sdk/src/Facebook/Exceptions/FacebookSDKException.php @@ -0,0 +1,33 @@ +