1 /*
2 # PostgreSQL Database Modeler (pgModeler)
3 #
4 # Copyright 2006-2020 - Raphael Araújo e Silva <raphael@pgmodeler.io>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # The complete text of GPLv3 is at LICENSE file on source code root directory.
16 # Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
17 */
18
19 /**
20 \ingroup libutils
21 \class Exception
22 \brief Catalogs all the errors raised by pgModeler
23 \note <strong>Creation date:</strong> 10/07/2006
24 */
25
26 #ifndef EXCEPTION_H
27 #define EXCEPTION_H
28
29 #include <QObject>
30 #include "doublenan.h"
31 #include <exception>
32 #include <signal.h>
33 #include <vector>
34 #include <deque>
35 #include <type_traits>
36
37 using namespace std;
38
39 //! \brief This function causes the provided enum to be converted to its underlying datatype
40 template<typename Enum>
enum_cast(Enum obj_type)41 constexpr std::underlying_type_t<Enum> enum_cast (Enum obj_type) noexcept
42 {
43 return static_cast<typename std::underlying_type_t<Enum>>(obj_type);
44 }
45
46 //! \brief This enum defines the global error codes used throughout the application
47 enum class ErrorCode: unsigned {
48 Custom,
49 AsgPseudoTypeColumn,
50 AsgInvalidPrecision,
51 AsgInvalidPrecisionTimestamp,
52 AsgNotAllocatedColumn,
53 RefColumnInvalidIndex,
54 AsgNotAllocattedObject,
55 AsgNotAllocatedSchema,
56 AsgObjectInvalidDefinition,
57 AsgDuplicatedObject,
58 AsgDuplicatedObjectContainer,
59 AsgObjectInvalidType,
60 RemObjectInvalidType,
61 ObtObjectInvalidType,
62 AsgEmptyNameTableReturnType,
63 AsgDuplicatedParameterFunction,
64 InsDuplicatedTableReturnType,
65 RefParameterInvalidIndex,
66 RefInvalidTriggerEvent,
67 AsgInvalidColumnTrigger,
68 AsgNotAllocatedFunction,
69 AsgInvalidTriggerFunction,
70 AsgFunctionInvalidParamCount,
71 AsgFunctionInvalidLanguage,
72 AsgEventTriggerFuncInvalidLang,
73 AsgNotAllocatedTable,
74 RefArgumentInvalidIndex,
75 AsgEmptyNameObject,
76 AsgInvalidNameObject,
77 AsgLongNameObject,
78 AsgInvalidSchemaObject,
79 AsgInvalidTablespaceObject,
80 AsgTablespaceInvalidObject,
81 AsgTablespaceInvalidConstraintType,
82 AsgInvalidRoleObject,
83 AsgRoleObjectInvalidType,
84 AsgCustomSQLObjectInvalidType,
85 RefFunctionInvalidType,
86 RefOperatorArgumentInvalidType,
87 RefOperatorInvalidType,
88 AsgValueInvalidRoleOptionType,
89 RefInvalidRoleType,
90 InsDuplicatedRole,
91 AsgRoleReferenceRedundancy,
92 AsgRoleMemberItself,
93 RefRoleInvalidIndex,
94 InsEmptyRuleCommand,
95 RefRuleCommandInvalidIndex,
96 InvInheritCopyPartRelationship,
97 AsgObjectBelongsAnotherTable,
98 AsgSchemaSequenceDiffersTableSchema,
99 AsgInvalidValueSequenceAttributes,
100 AsgInvalidSequenceMinValue,
101 AsgInvalidSequenceStartValue,
102 AsgInvalidSequenceIncrementValue,
103 AsgInvalidSequenceCacheValue,
104 AsgSeqOwnerTableDifferentSchema,
105 AsgSeqOwnerTableDifferentRole,
106 AsgInexistentSeqOwnerColumn,
107 AsgInvalidSeqOwnerColumn,
108 RefLabelInvalidIndex,
109 AllocationObjectInvalidType,
110 AsgFunctionInvalidReturnType,
111 AsgFunctionInvalidParameters,
112 AsgNotAllocatedLanguage,
113 AsgInvalidLanguageObject,
114 RefTypeInvalidIndex,
115 AsgNullTypeObject,
116 AsgInvalidTypeObject,
117 AsgEmptyDirectoryName,
118 ObtTypesInvalidQuantity,
119 InsDuplicatedItems,
120 InsInvalidTypeAttribute,
121 InsDuplicatedEnumerationItem,
122 InsInvalidEnumerationItem,
123 RefAttributeInvalidIndex,
124 RefEnumerationInvalidIndex,
125 AsgInvalidTypeConfiguration,
126 AsgInvalidOperatorArguments,
127 AsgInvalidOperatorTypes,
128 AsgReservedName,
129 AsgFunctionInvalidConfiguration,
130 AsgInvalidSupportStrategyNumber,
131 InsDuplicatedElement,
132 RefElementInvalidIndex,
133 RefObjectInvalidIndex,
134 RemNotAllocatedObject,
135 RemDirectReference,
136 RemInderectReference,
137 OprObjectInvalidType,
138 RefObjectInvalidType,
139 OprNotAllocatedObject,
140 InvLinkTablesNoPrimaryKey,
141 NotImplementedRelationshipType,
142 AsgInvalidExpressionObject,
143 AsgExistingPrimaryKeyTable,
144 InvIdentifierRelationship,
145 InvCopyRelationshipDuplicCols,
146 InvInheritRelationshipIncompCols,
147 InvInheritRelationshipIncompConstrs,
148 AsgObjectInvalidRelationshipType,
149 AsgForeignKeyRelationship,
150 RefObjectInexistsModel,
151 RefUserTypeInexistsModel,
152 AsgInvalidMaxSizeOpList,
153 FileDirectoryNotWritten,
154 FileNotWrittenInvalidDefinition,
155 InsDuplicatedRelationship,
156 InsRelationshipRedundancy,
157 RemInvalidatedObjects,
158 InvPrimaryKeyAllocation,
159 RefInvalidPrivilegeType,
160 InsDuplicatedRolePermission,
161 AsgInvalidPrivilegeObject,
162 AsgDuplicatedPermission,
163 PermissionRefInexistObject,
164 InvObjectAllocationNoSchema,
165 AsgTablespaceDuplicatedDirectory,
166 AsgInvalidSequenceTypeArray,
167 AsgSourceCodeFuncCLanguage,
168 AsgRefLibraryFuncLanguageNotC,
169 AsgInvalidCommutatorOperator,
170 AsgInvalidNegatorOperator,
171 InvUserTypeSelfReference,
172 AsgInvalidElementType,
173 AsgInvalidAlignmentType,
174 AsgInvalidNameTableRelNN,
175 InvUseSpecialPrimaryKey,
176 OprRelationshipAddedObject,
177 RemProtectedObject,
178 InvRedeclarationGroup,
179 InvGroupDeclaration,
180 DefNotDeclaredGroup,
181 DefEmptyGroup,
182 DefDuplicatedGroup,
183 InvGroupDeclarationNotDefined,
184 RefColObjectTabInvalidIndex,
185 RefRowObjectTabInvalidIndex,
186 OprReservedObject,
187 InvFuncConfigInvalidatesObject,
188 InvSQLScopeViewReference,
189 InvConstratintNoColumns,
190 ConfigurationNotLoaded,
191 DefaultConfigNotRestored,
192 ExportFailure,
193 PluginNotLoaded,
194 PluginsNotLoaded,
195 InvalidSyntax,
196 InvalidInstruction,
197 UnkownAttribute,
198 InvalidMetacharacter,
199 InvalidOperatorInExpression,
200 UndefinedAttributeValue,
201 InvalidAttribute,
202 AsgEmptyXMLBuffer,
203 FileDirectoryNotAccessed,
204 AsgEmptyDTDFile,
205 AsgEmptyDTDName,
206 LibXMLError,
207 OprNotAllocatedElementTree,
208 InvModelFileNotLoaded,
209 OprNotAllocatedElement,
210 OprInexistentElement,
211 AsgInvalidConnParameter,
212 OprNotAllocatedConnection,
213 ConnectionNotConfigured,
214 ConnectionAlreadyStablished,
215 ConnectionNotStablished,
216 AsgNotAllocatedSQLResult,
217 IncomprehensibleDBMSResponse,
218 DBMSFatalError,
219 RefTupleColumnInvalidIndex,
220 RefTupleColumnInvalidName,
221 RefInvalidTuple,
222 RefInvalidTupleColumn,
223 SQLCommandNotExecuted,
224 AsgInvalidViewDefExpression,
225 AsgSecondViewDefExpression,
226 MixingViewDefExprsReferences,
227 AsgInvalidCollationObject,
228 UnsupportedPKColsAddedByRel,
229 EmptyLCCollationAttributes,
230 ObjectReferencingItself,
231 AsgInvalidOpFamilyOpClassElem,
232 RefInvalidLikeOptionType,
233 InvCopyRelTableDefined,
234 InvPartRelPartitionedDefined,
235 InvRelTypeForPatitionTables,
236 InvTableTriggerInsteadOfFiring,
237 InvUsageTruncateOnTrigger,
238 InvUsageInsteadOfOnTrigger,
239 InvConstrTriggerNotAfterRow,
240 InvUsageAfterBeforeViewTrigger,
241 InvUsageInsteadOfUpdateTrigger,
242 AsgColumnNoParent,
243 InvUseConstraintTriggerAttribs,
244 RefInvalidFunctionIdTypeConfig,
245 AsgInvalidOpClassObject,
246 InvPostgreSQLVersion,
247 ValidationFailure,
248 ExtensionHandlingTypeImmutable,
249 InvAllocationFKRelationship,
250 AsgInvalidNamePattern,
251 RefInvalidNamePatternId,
252 InvUsageVariadicParamMode,
253 MixingIncompExportOptions,
254 MixingIncompDropOptions,
255 InvIdSwapSameObject,
256 InvIdSwapInvalidObjectType,
257 AsgWidgetAlreadyHasParent,
258 ObjectNotImported,
259 ModelFileNotLoaded,
260 InvColumnTableType,
261 OprInvalidElementId,
262 RefInvalidElementColorId,
263 AsgInvalidObjectType,
264 IncompColumnTypeForSequence,
265 InvUsageTempNamesExportOption,
266 InvConversionIntegerToSerial,
267 AsgInvalidEventTriggerVariable,
268 RowDataNotManipulated,
269 MalformedUnescapedValue,
270 UndoRedoOperationInvalidObject,
271 RequiredFieldsNotFilled,
272 InvRelationshipIdSwap,
273 InvInheritParentTableNotFound,
274 AsgEnumInvalidChars,
275 AsgEnumLongName,
276 ConnectionTimeout,
277 ConnectionBroken,
278 DropCurrentDBDefault,
279 NullPrimaryKeyColumn,
280 InvalidIdentityColumn,
281 RefInvalidAffectedCommand,
282 RefInvalidSpecialRole,
283 InvColumnCountPartRel,
284 InvPartitioningTypePartRel,
285 InvPartitionKeyCount,
286 PartKeyObjectInexistsModel,
287 AsgInvalidColumnPartitionKey,
288 RemColumnRefByPartitionKey,
289 AsgOptionInvalidName,
290 AsgInvalidNameObjReference,
291 AsgNotAllocatedObjectReference,
292 InsDuplicatedObjectReference,
293 ModelFileInvalidSize,
294 AsgInvalidObjectForeignTable,
295 InvRelTypeForeignTable,
296 InvCopyRelForeignTable,
297 InvDataDictDirectory,
298 InitialUserConfigNotCreated,
299 InvalidObjectFilter,
300 InvChildObjectTypeFilter,
301 InvChangelogEntryValues
302 };
303
304 class Exception {
305 private:
306 static constexpr unsigned ErrorCount=254;
307
308 /*! \brief Stores other exceptions before raise the 'this' exception.
309 This structure can be used to simulate a stack trace to improve the debug */
310 vector<Exception> exceptions;
311
312 //! \brief Stores the error messages and codes (names of errors) in string format
313 static QString messages[ErrorCount][2];
314
315 //! \brief Constants used to access the error details
316 static constexpr unsigned ErrorCodeId=0, ErrorMessage=1;
317
318 //! \brief Error type related to the exception
319 ErrorCode error_code;
320
321 //! \brief Formated error message
322 QString error_msg,
323 /*! \brief Holds the class name and method which was
324 triggered the exception. For this to be possible, at the time
325 instantiation of this class the G++ macro __ PRETTY_FUNCTION__
326 must be passed. This macro contains the format [RETURN][CLASS]::[METHOD][PARAMS] */
327 method,
328
329 //! \brief File where the exception was generated (Macro __ FILE__)
330 file,
331
332 /*! \brief Additional information (optional) may store any other
333 type of information that is interesting on attempt to resolve the error */
334 extra_info;
335
336 //! \brief Line of file where the exception were generated (Macro __LINE__)
337 int line;
338
339 //! \brief Configures the basic attributes of exception
340 void configureException(const QString &msg, ErrorCode error_code, const QString &method, const QString &file, int line, const QString &extra_info);
341
342 //! \brief Adds a exception to the list of exceptions
343 void addException(Exception &exception);
344
345 public:
346 static unsigned constexpr MaximumStackSize = 50;
347
348 Exception();
349 Exception(const QString &msg, const QString &method, const QString &file, int line, Exception *exception=nullptr, const QString &extra_info="");
350 Exception(const QString &msg, const QString &method, const QString &file, int line, vector<Exception> &exceptions, const QString &extra_info="");
351 Exception(const QString &msg, ErrorCode error_code, const QString &method, const QString &file, int line, Exception *exception=nullptr, const QString &extra_info="");
352 Exception(const QString &msg, ErrorCode error_code, const QString &method, const QString &file, int line, vector<Exception> &exceptions, const QString &extra_info="");
353 Exception(ErrorCode error_code, const QString &method, const QString &file, int line, Exception *exception=nullptr, const QString &extra_info="");
354 Exception(ErrorCode error_code, const QString &method, const QString &file, int line, vector<Exception> &exceptions, const QString &extra_info="");
355
~Exception(void)356 ~Exception(void){}
357 QString getErrorMessage();
358 static QString getErrorMessage(ErrorCode error_code);
359 static QString getErrorCode(ErrorCode error_code);
360 QString getMethod();
361 QString getFile();
362 QString getLine();
363 ErrorCode getErrorCode();
364 QString getExtraInfo();
365
366 //! \brief Gets the full exception stack
367 void getExceptionsList(vector<Exception> &list);
368
369 //! \brief Gets the exception stack in a formatted text
370 QString getExceptionsText();
371 };
372
373 #endif
374