ApplicationError.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\presentation;
12 
17 
18 /**
19  * Predefined error levels
20  */
21 define('ERROR_LEVEL_WARNING', 'warning');
22 define('ERROR_LEVEL_ERROR', 'error');
23 define('ERROR_LEVEL_FATAL', 'fatal');
24 
25 /**
26  * ApplicationError is used to signal errors that occur
27  * while processing a request.
28  *
29  * This class only allows to use predefined errors by
30  * using the ApplicationError::get() method.
31  * Errors are defined in the following way:
32 
33  * @code
34  * define('GENERAL_WARNING', serialize(array('GENERAL_WARNING',
35  * $message->getText('An unspecified warning occured.'), ERROR_LEVEL_WARNING)));
36  * @endcode
37  *
38  * To use the error in the code:
39  *
40  * @code
41  * $error = ApplicationError::get('GENERAL_WARNING', $mySpecificErrorData);
42  * @endcode
43  *
44  * @author ingo herwig <ingo@wemove.com>
45  */
47 
48  private $_code = null;
49  private $_message = null;
50  private $_level = null;
51  private $_data = null;
52 
53  /**
54  * Constructor
55  * @param $code An error code, describing the type of error
56  * @param $message An error message which is displayed to the user
57  * @param $level One of the ERROR_LEVEL constants
58  * @param $data Some error codes required to transmit further information
59  * to the client (optional, default: _null_)
60  */
61  private function __construct($code, $message, $level, $data=null) {
62  $this->_code = $code;
63  $this->_message = $message;
64  $this->_level = $level;
65  $this->_data = $data;
66  }
67 
68  /**
69  * Get the error code
70  * @return The code
71  */
72  public function getCode() {
73  return $this->_code;
74  }
75 
76  /**
77  * Get the error message
78  * @return The message
79  */
80  public function getMessage() {
81  return $this->_message;
82  }
83 
84  /**
85  * Set the error data
86  * @param $data Some error codes require to transmit
87  * further information to the client
88  */
89  public function setData($data) {
90  $this->_data = $data;
91  }
92 
93  /**
94  * Get the error data
95  * @return The data
96  */
97  public function getData() {
98  return $this->_data;
99  }
100 
101  /**
102  * Get a string representation of the error
103  * @return String
104  */
105  public function __toString() {
106  $str = strtoupper($this->_level).": ".$this->_code.": ".$this->_message;
107  if ($this->_data) {
108  $str .= " Data: ".StringUtil::getDump($this->_data);
109  }
110  return $str;
111  }
112 
113  /**
114  * Factory method for retrieving a predefind error instance.
115  * @param $code An error code
116  * @param $data Some error codes required to transmit further information
117  * to the client (optional, default: _null_)
118  * @return ApplicationError
119  */
120  public static function get($code, $data=null) {
121  if (defined($code)) {
122  $def = unserialize(constant($code));
123  return new ApplicationError($def[0], $def[1], $def[2], $data);
124  }
125  else {
126  throw new IllegalArgumentException("The error code '".$code."' is not defined");
127  }
128  }
129 
130  /**
131  * Factory method for creating a general error instance.
132  * @param $message Error message
133  * @return ApplicationError
134  */
135  public static function getGeneral($message) {
136  return new ApplicationError('GENERAL_ERROR', $message, ERROR_LEVEL_ERROR);
137  }
138 
139  /**
140  * Factory method for transforming an exception into an ApplicationError instance.
141  * @param $ex Exception
142  * @return ApplicationError
143  */
144  public static function fromException(\Exception $ex) {
145  if ($ex instanceof ApplicationException) {
146  return $ex->getError();
147  }
148  return new ApplicationError('GENERAL_ERROR', $ex->getMessage(), ERROR_LEVEL_ERROR);
149  }
150 }
151 
152 /**
153  * Predefined errors
154  */
156 define('GENERAL_WARNING', serialize(array('GENERAL_WARNING',
157  $message->getText('An unspecified warning occured.'), ERROR_LEVEL_WARNING)));
158 define('GENERAL_ERROR', serialize(array('GENERAL_ERROR',
159  $message->getText('An unspecified error occured.', ERROR_LEVEL_ERROR))));
160 define('GENERAL_FATAL', serialize(array('GENERAL_FATAL',
161  $message->getText('An unspecified fatal error occured.'), ERROR_LEVEL_FATAL)));
162 
163 define('ACTION_INVALID', serialize(array('ACTION_INVALID',
164  $message->getText('The requested action is unknown.'), ERROR_LEVEL_ERROR)));
165 define('SESSION_INVALID', serialize(array('SESSION_INVALID',
166  $message->getText('The session is invalid.'), ERROR_LEVEL_ERROR)));
167 define('PARAMETER_MISSING', serialize(array('PARAMETER_MISSING',
168  $message->getText('One or more parameters are missing.'), ERROR_LEVEL_ERROR)));
169 define('PARAMETER_INVALID', serialize(array('PARAMETER_INVALID',
170  $message->getText('One or more parameters are invalid.'), ERROR_LEVEL_ERROR)));
171 define('OID_INVALID', serialize(array('OID_INVALID',
172  $message->getText('One or more object ids are invalid.'), ERROR_LEVEL_ERROR)));
173 define('CLASS_NAME_INVALID', serialize(array('CLASS_NAME_INVALID',
174  $message->getText('One or more classes are invalid.'), ERROR_LEVEL_ERROR)));
175 
176 define('AUTHENTICATION_FAILED', serialize(array('AUTHENTICATION_FAILED',
177  $message->getText('Authentication failed.'), ERROR_LEVEL_ERROR)));
178 
179 define('LIMIT_NEGATIVE', serialize(array('LIMIT_NEGATIVE',
180  $message->getText('The passed limit is a negative number.'), ERROR_LEVEL_WARNING)));
181 define('OFFSET_OUT_OF_BOUNDS', serialize(array('OFFSET_OUT_OF_BOUNDS',
182  $message->getText('The passed offset is negative or greater than the number of entries matching the parameters.'), ERROR_LEVEL_WARNING)));
183 define('SORT_FIELD_UNKNOWN', serialize(array('SORT_FIELD_UNKNOWN',
184  $message->getText('The passed sortFieldName is no valid attribute of the passed class.'), ERROR_LEVEL_ERROR)));
185 define('SORT_DIRECTION_UNKNOWN', serialize(array('SORT_DIRECTION_UNKNOWN',
186  $message->getText('The passed sortDirection has an invalid value.'), ERROR_LEVEL_ERROR)));
187 
188 define('DEPTH_INVALID', serialize(array('DEPTH_INVALID',
189  $message->getText('The passed depth is a negative number other than -1.'), ERROR_LEVEL_ERROR)));
190 
191 define('ATTRIBUTE_NAME_INVALID', serialize(array('ATTRIBUTE_NAME_INVALID',
192  $message->getText('The attribute name passed cannot be found in the selected class.'), ERROR_LEVEL_ERROR)));
193 define('ATTRIBUTE_VALUE_INVALID', serialize(array('ATTRIBUTE_VALUE_INVALID',
194  $message->getText('The attribute value passed is invalid for the attribute.'), ERROR_LEVEL_ERROR)));
195 define('CONCURRENT_UPDATE', serialize(array('CONCURRENT_UPDATE',
196  $message->getText('The server detected a concurrent update.'), ERROR_LEVEL_ERROR)));
197 
198 define('ROLE_INVALID', serialize(array('ROLE_INVALID',
199  $message->getText('The role passed cannot be found in the selected source class.'), ERROR_LEVEL_ERROR)));
200 define('ASSOCIATION_INVALID', serialize(array('ASSOCIATION_INVALID',
201  $message->getText('There is no association between the source and the target class.'), ERROR_LEVEL_ERROR)));
202 define('ASSOCIATION_NOT_FOUND', serialize(array('ASSOCIATION_NOT_FOUND',
203  $message->getText('No current association matching the input parameters can be found.'), ERROR_LEVEL_WARNING)));
204 
205 define('SEARCH_NOT_SUPPORTED', serialize(array('SEARCH_NOT_SUPPORTED',
206  $message->getText('There selected class does not support searching.'), ERROR_LEVEL_ERROR)));
207 
208 define('ORDER_UNDEFINED', serialize(array('ORDER_UNDEFINED',
209  $message->getText('There is no order defined for the root object.'), ERROR_LEVEL_WARNING)));
210 
211 define('REFERENCE_INVALID', serialize(array('REFERENCE_INVALID',
212  $message->getText('There reference object cannot be found in the container object.'), ERROR_LEVEL_ERROR)));
213 define('ORDER_NOT_SUPPORTED', serialize(array('ORDER_NOT_SUPPORTED',
214  $message->getText('The container class does not support ordered references.'), ERROR_LEVEL_ERROR)));
215 
216 define('CLASSES_DO_NOT_MATCH', serialize(array('CLASSES_DO_NOT_MATCH',
217  $message->getText('The classes of insertOid and referenceOid do not match.'), ERROR_LEVEL_ERROR)));
218 
219 define('HISTORY_NOT_SUPPORTED', serialize(array('HISTORY_NOT_SUPPORTED',
220  $message->getText('There selected class does not support history.'), ERROR_LEVEL_ERROR)));
221 
222 define('PERMISSION_DENIED', serialize(array('PERMISSION_DENIED',
223  $message->getText('The user does not have the permission to perform this action.'), ERROR_LEVEL_ERROR)));
224 
225 define('OBJECT_IS_LOCKED', serialize(array('OBJECT_IS_LOCKED',
226  $message->getText('The object is currently locked by another user.'), ERROR_LEVEL_ERROR)));
227 ?>
__toString()
Get a string representation of the error.
static getGeneral($message)
Factory method for creating a general error instance.
const ERROR_LEVEL_WARNING
Predefined error levels.
IllegalArgumentException signals an exception in method arguments.
Presentation related interfaces and classes.
Definition: namespaces.php:59
setData($data)
Set the error data.
static getInstance($name, $dynamicConfiguration=array())
static fromException(\Exception $ex)
Factory method for transforming an exception into an ApplicationError instance.
$message
Predefined errors.
ApplicationError is used to signal errors that occur while processing a request.
ApplicationException signals a general application exception.