8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
21 * @version $Id: ServiceException.php 16971 2009-07-22 18:05:45Z mikaelkael $
28 require_once 'Zend/Exception.php';
31 * Zend_Gdata_Gapps_Error
33 require_once 'Zend/Gdata/Gapps/Error.php';
36 * Gdata Gapps Exception class. This is thrown when an
37 * AppsForYourDomainErrors message is received from the Google Apps
40 * Several different errors may be represented by this exception. For a list
41 * of error codes available, see getErrorCode.
46 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
47 * @license http://framework.zend.com/license/new-bsd New BSD License
49 class Zend_Gdata_Gapps_ServiceException
extends Zend_Exception
52 protected $_rootElement = "AppsForYourDomainErrors";
55 * Array of Zend_Gdata_Error objects indexed by error code.
59 protected $_errors = array();
62 * Create a new ServiceException.
64 * @return array An array containing a collection of
65 * Zend_Gdata_Gapps_Error objects.
67 public function __construct($errors = null) {
68 parent
::__construct("Server errors encountered");
69 if ($errors !== null) {
70 $this->setErrors($errors);
75 * Add a single Error object to the list of errors received by the
78 * @param Zend_Gdata_Gapps_Error $error An instance of an error returned
79 * by the server. The error's errorCode must be set.
80 * @throws Zend_Gdata_App_Exception
82 public function addError($error) {
83 // Make sure that we don't try to index an error that doesn't
84 // contain an index value.
85 if ($error->getErrorCode() == null) {
86 require_once 'Zend/Gdata/App/Exception.php';
87 throw new Zend_Gdata_App_Exception("Error encountered without corresponding error code.");
90 $this->_errors
[$error->getErrorCode()] = $error;
94 * Set the list of errors as sent by the server inside of an
95 * AppsForYourDomainErrors tag.
97 * @param array $array An associative array containing a collection of
98 * Zend_Gdata_Gapps_Error objects. All errors must have their
99 * errorCode value set.
100 * @throws Zend_Gdata_App_Exception
102 public function setErrors($array) {
103 $this->_errors
= array();
104 foreach ($array as $error) {
105 $this->addError($error);
110 * Get the list of errors as sent by the server inside of an
111 * AppsForYourDomainErrors tag.
113 * @return array An associative array containing a collection of
114 * Zend_Gdata_Gapps_Error objects, indexed by error code.
116 public function getErrors() {
117 return $this->_errors
;
121 * Return the Error object associated with a specific error code.
123 * @return Zend_Gdata_Gapps_Error The Error object requested, or null
126 public function getError($errorCode) {
127 if (array_key_exists($errorCode, $this->_errors
)) {
128 $result = $this->_errors
[$errorCode];
136 * Check whether or not a particular error code was returned by the
139 * @param integer $errorCode The error code to check against.
140 * @return boolean Whether or not the supplied error code was returned
143 public function hasError($errorCode) {
144 return array_key_exists($errorCode, $this->_errors
);
148 * Import an AppsForYourDomain error from XML.
150 * @param string $string The XML data to be imported
151 * @return Zend_Gdata_Gapps_ServiceException Provides a fluent interface.
152 * @throws Zend_Gdata_App_Exception
154 public function importFromString($string) {
156 // Check to see if an AppsForYourDomainError exists
158 // track_errors is temporarily enabled so that if an error
159 // occurs while parsing the XML we can append it to an
160 // exception by referencing $php_errormsg
161 @ini_set
('track_errors', 1);
162 $doc = new DOMDocument();
163 $success = @$doc->loadXML($string);
164 @ini_restore
('track_errors');
167 require_once 'Zend/Gdata/App/Exception.php';
168 // $php_errormsg is automatically generated by PHP if
169 // an error occurs while calling loadXML(), above.
170 throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $php_errormsg");
173 // Ensure that the outermost node is an AppsForYourDomain error.
174 // If it isn't, something has gone horribly wrong.
175 $rootElement = $doc->getElementsByTagName($this->_rootElement
)->item(0);
177 require_once 'Zend/Gdata/App/Exception.php';
178 throw new Zend_Gdata_App_Exception('No root <' . $this->_rootElement
. '> element found, cannot parse feed.');
181 foreach ($rootElement->childNodes
as $errorNode) {
182 if (!($errorNode instanceof DOMText
)) {
183 $error = new Zend_Gdata_Gapps_Error();
184 $error->transferFromDom($errorNode);
185 $this->addError($error);
190 require_once 'Zend/Gdata/App/Exception.php';
191 throw new Zend_Gdata_App_Exception('XML passed to transferFromXML cannot be null');
197 * Get a human readable version of this exception.
201 public function __toString() {
202 $result = "The server encountered the following errors processing the request:";
203 foreach ($this->_errors
as $error) {
204 $result .= "\n" . $error->__toString();