4 * OAuth2.0 draft v10 exception handling.
6 * @author Originally written by Naitik Shah <naitik@facebook.com>.
7 * @author Update to draft v10 by Edison Wong <hswong3i@pantarei-design.com>.
9 * @sa <a href="https://github.com/facebook/php-sdk">Facebook PHP SDK</a>.
11 class OAuth2Exception extends Exception {
14 * The result from the API server that represents the exception information.
19 * Make a new API Exception with the given result.
22 * The result from the API server.
24 public function __construct($result) {
25 $this->result = $result;
27 $code = isset($result['code']) ? $result['code'] : 0;
29 if (isset($result['error'])) {
30 // OAuth 2.0 Draft 10 style
31 $message = $result['error'];
33 elseif (isset($result['message'])) {
35 $message = $result['message'];
38 $message = 'Unknown Error. Check getResult()';
41 parent::__construct($message, $code);
45 * Return the associated result object returned by the API server.
48 * The result from the API server.
50 public function getResult() {
55 * Returns the associated type for the error. This will default to
56 * 'Exception' when a type is not available.
59 * The type for the error.
61 public function getType() {
62 if (isset($this->result['error'])) {
63 $message = $this->result['error'];
64 if (is_string($message)) {
65 // OAuth 2.0 Draft 10 style
73 * To make debugging easier.
76 * The string representation of the error.
78 public function __toString() {
79 $str = $this->getType() . ': ';
80 if ($this->code != 0) {
81 $str .= $this->code . ': ';
83 return $str . $this->message;