From b06588ffa1c925d0a008a34bf8fa5c316b964b87 Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Thu, 20 Oct 2011 15:57:35 +0200 Subject: Initial work adding oauth to api --- library/oauth2-php/lib/OAuth2Exception.inc | 85 ++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 library/oauth2-php/lib/OAuth2Exception.inc (limited to 'library/oauth2-php/lib/OAuth2Exception.inc') diff --git a/library/oauth2-php/lib/OAuth2Exception.inc b/library/oauth2-php/lib/OAuth2Exception.inc new file mode 100644 index 000000000..8dc046974 --- /dev/null +++ b/library/oauth2-php/lib/OAuth2Exception.inc @@ -0,0 +1,85 @@ +. + * @author Update to draft v10 by Edison Wong . + * + * @sa Facebook PHP SDK. + */ +class OAuth2Exception extends Exception { + + /** + * The result from the API server that represents the exception information. + */ + protected $result; + + /** + * Make a new API Exception with the given result. + * + * @param $result + * The result from the API server. + */ + public function __construct($result) { + $this->result = $result; + + $code = isset($result['code']) ? $result['code'] : 0; + + if (isset($result['error'])) { + // OAuth 2.0 Draft 10 style + $message = $result['error']; + } + elseif (isset($result['message'])) { + // cURL style + $message = $result['message']; + } + else { + $message = 'Unknown Error. Check getResult()'; + } + + parent::__construct($message, $code); + } + + /** + * Return the associated result object returned by the API server. + * + * @returns + * The result from the API server. + */ + public function getResult() { + return $this->result; + } + + /** + * Returns the associated type for the error. This will default to + * 'Exception' when a type is not available. + * + * @return + * The type for the error. + */ + public function getType() { + if (isset($this->result['error'])) { + $message = $this->result['error']; + if (is_string($message)) { + // OAuth 2.0 Draft 10 style + return $message; + } + } + return 'Exception'; + } + + /** + * To make debugging easier. + * + * @returns + * The string representation of the error. + */ + public function __toString() { + $str = $this->getType() . ': '; + if ($this->code != 0) { + $str .= $this->code . ': '; + } + return $str . $this->message; + } +} -- cgit v1.2.3