1: <?php
2: /**
3: * This file contains the implementation of the class SmartException. It extends the standard
4: * exception by the string code and type of excetion.
5: *
6: * Since the error texts can be localized, the unique code of the error might be important fo using
7: * in comparison.
8: * The type of the error might be useful for decision how to show the error on the client. If it is a
9: * user error, the full error texts should be shown. If it is a programming error, the detailed text should
10: * be shown only in the debug mode to prevent that the hackers get sensible information about the system.
11: *
12: * @package System
13: *
14: * @author Oleg Schildt
15: */
16:
17: namespace SmartFactory;
18:
19: /**
20: * Class for extended exception used in the SmartFactory library.
21: *
22: * @author Oleg Schildt
23: */
24: class SmartException extends \Exception
25: {
26: /**
27: * Constant for the error code "system_error".
28: *
29: * @var string
30: *
31: * @author Oleg Schildt
32: */
33: const string ERR_CODE_SYSTEM_ERROR = "system_error";
34:
35: /**
36: * Constant for the error code "config_error".
37: *
38: * @var string
39: *
40: * @author Oleg Schildt
41: */
42: const string ERR_CODE_CONFIG_ERROR = "config_error";
43:
44: /**
45: * Constant for the error code "missing_request_data".
46: *
47: * @var string
48: *
49: * @author Oleg Schildt
50: */
51: const string ERR_CODE_MISSING_REQUEST_DATA = "missing_request_data";
52:
53: /**
54: * Constant for the error code "invalid_request_data".
55: *
56: * @var string
57: *
58: * @author Oleg Schildt
59: */
60: const string ERR_CODE_INVALID_REQUEST_DATA = "invalid_request_data";
61:
62: /**
63: * Constant for the error code "invalid_content_type".
64: *
65: * @var string
66: *
67: * @author Oleg Schildt
68: */
69: const string ERR_CODE_INVALID_CONTENT_TYPE = "invalid_content_type";
70:
71: /**
72: * Constant for the error code "json_parse_error".
73: *
74: * @var string
75: *
76: * @author Oleg Schildt
77: */
78: const string ERR_CODE_JSON_PARSE_ERROR = "json_parse_error";
79:
80: /**
81: * Constant for the error code "xml_parse_error".
82: *
83: * @var string
84: *
85: * @author Oleg Schildt
86: */
87: const string ERR_CODE_XML_PARSE_ERROR = "xml_parse_error";
88:
89: /**
90: * Constant for the error code "path_access_error".
91: *
92: * @var string
93: *
94: * @author Oleg Schildt
95: */
96: const string ERR_CODE_PATH_ACCESS_ERROR = "path_access_error";
97:
98: /**
99: * Constant for the error code "file_write_error".
100: *
101: * @var string
102: *
103: * @author Oleg Schildt
104: */
105: const string ERR_CODE_FILE_WRITE_ERROR = "file_write_error";
106:
107: /**
108: * Constant for the error code "not_found".
109: *
110: * @var string
111: *
112: * @author Oleg Schildt
113: */
114: const string ERR_CODE_NOT_FOUND = "not_found";
115:
116: /**
117: * Internal variable for storing the error element.
118: *
119: * @var string
120: *
121: * @see SmartException::getErrorElement()
122: *
123: * @author Oleg Schildt
124: */
125: protected string $error_element = "";
126:
127: /**
128: * Internal variable for storing the error.
129: *
130: * @var string
131: *
132: * @see SmartException::getErrorCode()
133: *
134: * @author Oleg Schildt
135: */
136: protected string $error_code = SmartException::ERR_CODE_SYSTEM_ERROR;
137:
138: /**
139: * Internal variable for storing the error details.
140: *
141: * The error details might be useful if the error message translations are provided on the client, not
142: * on the server, and the error message should contain some details that may vary from case to case.
143: * In that case, the servers return the error message id instead of final text and the details, the client
144: * uses the error message id, gets the final translated text and substitutes the parameters through the details.
145: *
146: * @var array
147: *
148: * @see SmartException::getErrorDetails()
149: *
150: * @author Oleg Schildt
151: */
152: protected array $error_details = [];
153:
154: /**
155: * Internal variable for storing the additional technical information.
156: *
157: * @var string
158: *
159: * @see SmartException::getTechnicalInfo()
160: *
161: * @author Oleg Schildt
162: */
163: protected string $technical_info = "";
164:
165: /**
166: * Constructor.
167: *
168: * @param string $message
169: * Error message.
170: *
171: * @param string $error_code
172: * Error code.
173: *
174: * Since the error texts can be localized, the unique code of the error might be important fo using
175: * in comparison.
176: *
177: * @param string $error_element
178: * Error element.
179: *
180: * @param array $error_details
181: * The details might be useful if the message translations are provided on the client, not
182: * on the server, and the message should contain some details that may vary from case to case.
183: * In that case, the servers return the message id instead of final text and the details, the client
184: * uses the message id, gets the final translated text and substitutes the parameters through the details.
185: *
186: * @param string $technical_info
187: * Additional technical information. It can be used if it is a programming error and displayed only if the debug mode is active.
188: *
189: * @author Oleg Schildt
190: */
191: public function __construct(string $message, string $error_code = SmartException::ERR_CODE_SYSTEM_ERROR, string $error_element = "", array $error_details = [], string $technical_info = "")
192: {
193: parent::__construct($message);
194:
195: $this->error_code = $error_code;
196: $this->error_element = $error_element;
197: $this->error_details = $error_details;
198: $this->technical_info = $technical_info;
199: }
200:
201: /**
202: * Returns the string error code of the exception.
203: *
204: * Since the error texts can be localized, the unique code of the error might be important fo using
205: * in comparison.
206: *
207: * @return string
208: * Returns the error code.
209: *
210: * @see SmartException::getErrorDetails()
211: * @see SmartException::getErrorElement()
212: * @see SmartException::getTechnicalInfo()
213: *
214: * @author Oleg Schildt
215: */
216: public function getErrorCode(): string
217: {
218: return $this->error_code;
219: }
220:
221: /**
222: * Sets the error element.
223: *
224: * @param string $error_element
225: * Error element.
226: *
227: * @return void
228: *
229: * @see SmartException::getErrorElement()
230: *
231: * @author Oleg Schildt
232: */
233: public function setErrorElement(string $error_element): void
234: {
235: $this->error_element = $error_element;
236: }
237:
238: /**
239: * Returns the error element.
240: *
241: * @return string
242: * Returns the error element.
243: *
244: * @see SmartException::setErrorElement()
245: * @see SmartException::getErrorCode()
246: * @see SmartException::getErrorDetails()
247: * @see SmartException::getTechnicalInfo()
248: *
249: * @author Oleg Schildt
250: */
251: public function getErrorElement(): string
252: {
253: return $this->error_element;
254: }
255:
256: /**
257: * Returns the error details of the exception.
258: *
259: * The error details might be useful if the error message translations are provided on the client, not
260: * on the server, and the error message should contain some details that may vary from case to case.
261: * In that case, the servers return the error message id instead of final text and the details, the client
262: * uses the error message id, gets the final translated text and substitutes the parameters through the details.
263: *
264: * @return array
265: * Returns the error details.
266: *
267: * @see SmartException::getErrorCode()
268: * @see SmartException::getErrorElement()
269: * @see SmartException::getTechnicalInfo()
270: *
271: * @author Oleg Schildt
272: */
273: public function getErrorDetails(): array
274: {
275: return $this->error_details;
276: }
277:
278: /**
279: * Returns the technical information for the error.
280: *
281: * @return string
282: * Returns the technical information for the error.
283: *
284: * @see SmartException::getErrorCode()
285: * @see SmartException::getErrorElement()
286: * @see SmartException::getTechnicalInfo()
287: *
288: * @author Oleg Schildt
289: */
290: public function getTechnicalInfo(): string
291: {
292: return $this->technical_info;
293: }
294: } // SmartException