]> git.mxchange.org Git - friendica.git/blob - library/oauth2-php/lib/OAuth2Exception.inc
Merge pull request #4234 from annando/participation-2
[friendica.git] / library / oauth2-php / lib / OAuth2Exception.inc
1 <?php
2
3 /**
4  * OAuth2.0 draft v10 exception handling.
5  *
6  * @author Originally written by Naitik Shah <naitik@facebook.com>.
7  * @author Update to draft v10 by Edison Wong <hswong3i@pantarei-design.com>.
8  *
9  * @sa <a href="https://github.com/facebook/php-sdk">Facebook PHP SDK</a>.
10  */
11 class OAuth2Exception extends Exception {
12
13   /**
14    * The result from the API server that represents the exception information.
15    */
16   protected $result;
17
18   /**
19    * Make a new API Exception with the given result.
20    *
21    * @param $result
22    *   The result from the API server.
23    */
24   public function __construct($result) {
25     $this->result = $result;
26
27     $code = isset($result['code']) ? $result['code'] : 0;
28
29     if (isset($result['error'])) {
30       // OAuth 2.0 Draft 10 style
31       $message = $result['error'];
32     }
33     elseif (isset($result['message'])) {
34       // cURL style
35       $message = $result['message'];
36     }
37     else {
38       $message = 'Unknown Error. Check getResult()';
39     }
40
41     parent::__construct($message, $code);
42   }
43
44   /**
45    * Return the associated result object returned by the API server.
46    *
47    * @returns
48    *   The result from the API server.
49    */
50   public function getResult() {
51     return $this->result;
52   }
53
54   /**
55    * Returns the associated type for the error. This will default to
56    * 'Exception' when a type is not available.
57    *
58    * @return
59    *   The type for the error.
60    */
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
66         return $message;
67       }
68     }
69     return 'Exception';
70   }
71
72   /**
73    * To make debugging easier.
74    *
75    * @returns
76    *   The string representation of the error.
77    */
78   public function __toString() {
79     $str = $this->getType() . ': ';
80     if ($this->code != 0) {
81       $str .= $this->code . ': ';
82     }
83     return $str . $this->message;
84   }
85 }