1 <?php
2
3 /**
4 * ArangoDB PHP client: server exception
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9 */
10
11 namespace triagens\ArangoDb;
12
13 /**
14 * Server-Exception
15 *
16 * This exception type will be thrown by the client when the server returns an
17 * error in response to a client request.
18 *
19 * The exception code is the HTTP status code as returned by
20 * the server.
21 * In case the server provides additional details
22 * about the error, these details can be queried using the
23 * getDetails() function.<br>
24 * <br>
25 *
26 * @package triagens\ArangoDb
27 * @since 0.2
28 */
29 class ServerException extends
30 Exception
31 {
32 /**
33 * Optional details for the exception
34 *
35 * @param array
36 */
37 private $_details = [];
38
39 /**
40 * Error number index
41 */
42 const ENTRY_CODE = 'errorNum';
43
44 /**
45 * Error message index
46 */
47 const ENTRY_MESSAGE = 'errorMessage';
48
49 /**
50 * Return a string representation of the exception
51 *
52 * @return string - string representation
53 */
54 public function __toString()
55 {
56 return __CLASS__ . ': ' . $this->getServerCode() . ' ' . $this->getMessage();
57 }
58
59 /**
60 * Set exception details
61 *
62 * If the server provides additional details about the error
63 * that occurred, they will be put here.
64 *
65 * @param array $details - array of exception details
66 *
67 * @return void
68 */
69 public function setDetails(array $details)
70 {
71 $this->_details = $details;
72 }
73
74 /**
75 * Get exception details
76 *
77 * If the server has provided additional details about the error
78 * that occurred, they can be queries using the method
79 *
80 * @return array - array of details
81 */
82 public function getDetails()
83 {
84 return $this->_details;
85 }
86
87 /**
88 * Get server error code
89 *
90 * If the server has provided additional details about the error
91 * that occurred, this will return the server error code
92 *
93 * @return int - server error code
94 */
95 public function getServerCode()
96 {
97 if (isset($this->_details[self::ENTRY_CODE])) {
98 return $this->_details[self::ENTRY_CODE];
99 }
100
101 return $this->getCode();
102 }
103
104 /**
105 * Get server error message
106 *
107 * If the server has provided additional details about the error
108 * that occurred, this will return the server error string
109 *
110 * @return string - server error message
111 */
112 public function getServerMessage()
113 {
114 if (isset($this->_details[self::ENTRY_MESSAGE])) {
115 return $this->_details[self::ENTRY_MESSAGE];
116 }
117
118 return null;
119 }
120 }
121