Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12694 anikendra 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
class OAuth2_Exception extends Exception {
10
 
11
	/**
12
	 * The result from the API server that represents the exception information.
13
	 */
14
	protected $result;
15
 
16
	/**
17
	 * Make a new API Exception with the given result.
18
	 *
19
	 * @param $result
20
	 *   The result from the API server.
21
	 */
22
	public function __construct($result)
23
	{
24
		$this->result = $result;
25
 
26
		$code = isset($result['code']) ? $result['code'] : 0;
27
 
28
		if (isset($result['error']))
29
		{
30
			// OAuth 2.0 Draft 10 style
31
			$message = $result['error'];
32
		}
33
		elseif (isset($result['message']))
34
		{
35
			// cURL style
36
			$message = $result['message'];
37
		}
38
		else
39
		{
40
			$message = 'Unknown Error.';
41
		}
42
 
43
		parent::__construct($message, $code);
44
	}
45
 
46
	/**
47
	* Returns the associated type for the error. This will default to
48
	* 'Exception' when a type is not available.
49
	*
50
	* @return
51
	*   The type for the error.
52
	*/
53
	public function getType()
54
	{
55
		if (isset($this->result['error']))
56
		{
57
			$message = $this->result['error'];
58
			if (is_string($message))
59
			{
60
				// OAuth 2.0 Draft 10 style
61
				return $message;
62
			}
63
		}
64
		return 'Exception';
65
	}
66
 
67
	/**
68
	 * To make debugging easier.
69
	 *
70
	 * @returns
71
	 *   The string representation of the error.
72
	 */
73
	public function __toString()
74
	{
75
		$str = $this->getType() . ': ';
76
		if ($this->code != 0)
77
		{
78
			$str .= $this->code . ': ';
79
		}
80
		return $str . $this->message;
81
	}
82
 
83
}