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 | } elseif (isset($result['message'])) { |
---|
33 | // cURL style |
---|
34 | $message = $result['message']; |
---|
35 | } else { |
---|
36 | $message = 'Unknown Error. Check getResult()'; |
---|
37 | } |
---|
38 | |
---|
39 | parent::__construct($message, $code); |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Return the associated result object returned by the API server. |
---|
44 | * |
---|
45 | * @returns |
---|
46 | * The result from the API server. |
---|
47 | */ |
---|
48 | public function getResult() { |
---|
49 | return $this->result; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Returns the associated type for the error. This will default to |
---|
54 | * 'Exception' when a type is not available. |
---|
55 | * |
---|
56 | * @return |
---|
57 | * The type for the error. |
---|
58 | */ |
---|
59 | public function getType() { |
---|
60 | if (isset($this->result['error'])) { |
---|
61 | $message = $this->result['error']; |
---|
62 | if (is_string($message)) { |
---|
63 | // OAuth 2.0 Draft 10 style |
---|
64 | return $message; |
---|
65 | } |
---|
66 | } |
---|
67 | return 'Exception'; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * To make debugging easier. |
---|
72 | * |
---|
73 | * @returns |
---|
74 | * The string representation of the error. |
---|
75 | */ |
---|
76 | public function __toString() { |
---|
77 | $str = $this->getType() . ': '; |
---|
78 | if ($this->code != 0) { |
---|
79 | $str .= $this->code . ': '; |
---|
80 | } |
---|
81 | return $str . $this->message; |
---|
82 | } |
---|
83 | } |
---|