File indexing completed on 2025-02-09 04:28:37
0001 /* 0002 This file is part of the KTextTemplate library 0003 0004 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com> 0005 0006 SPDX-License-Identifier: LGPL-2.1-or-later 0007 0008 */ 0009 0010 #ifndef KTEXTTEMPLATE_EXCEPTION_H 0011 #define KTEXTTEMPLATE_EXCEPTION_H 0012 0013 #include "ktexttemplate_export.h" 0014 0015 #include <QString> 0016 0017 #include <exception> 0018 0019 namespace KTextTemplate 0020 { 0021 0022 /** 0023 Types of errors that can occur while using %KTextTemplate 0024 */ 0025 enum Error { 0026 NoError, 0027 EmptyVariableError, 0028 EmptyBlockTagError, 0029 InvalidBlockTagError, 0030 UnclosedBlockTagError, 0031 UnknownFilterError, 0032 TagSyntaxError, 0033 // VariableSyntaxError, 0034 0035 VariableNotInContext, 0036 ObjectReturnTypeInvalid, 0037 CompileFunctionError 0038 }; 0039 0040 /// @headerfile exception.h <KTextTemplate/Exception> 0041 0042 /** 0043 @brief An exception for use when implementing template tags. 0044 0045 The **%Exception** class can be used when implementing 0046 AbstractNodeFactory::getNode. An exception can be thrown to indicate that 0047 the syntax of a particular tag is invalid. 0048 0049 For example, the following template markup should throw an error because the 0050 include tag should have exactly one argument: 0051 0052 @code 0053 <div> 0054 {% include %} 0055 </div> 0056 @endcode 0057 0058 The corresponding implementation of IncludeNodeFactory::getNode is 0059 0060 @code 0061 QStringList tagContents = smartSplit( tagContent ); 0062 0063 if ( tagContents.size() != 2 ) 0064 throw KTextTemplate::Exception( TagSyntaxError, 0065 "Error: Include tag takes exactly one argument" ); 0066 0067 // The item at index 0 in the list is the tag name, "include" 0068 QString includeName = tagContents.at( 1 ); 0069 @endcode 0070 0071 @author Stephen Kelly <steveire@gmail.com> 0072 */ 0073 class KTEXTTEMPLATE_EXPORT Exception 0074 { 0075 public: 0076 /** 0077 Creates an exception for the error @p errorCode and the verbose 0078 message @p what 0079 */ 0080 Exception(Error errorCode, const QString &what) 0081 : m_errorCode(errorCode) 0082 , m_what(what) 0083 { 0084 } 0085 0086 virtual ~Exception() throw() 0087 { 0088 } 0089 0090 #ifndef K_DOXYGEN 0091 /** 0092 @internal 0093 0094 Returns the verbose message for the exception. 0095 */ 0096 const QString what() const throw() 0097 { 0098 return m_what; 0099 } 0100 0101 /** 0102 @internal 0103 0104 Returns the error code for the exception. 0105 */ 0106 Error errorCode() const 0107 { 0108 return m_errorCode; 0109 } 0110 #endif 0111 0112 private: 0113 Error m_errorCode; 0114 QString m_what; 0115 }; 0116 } 0117 0118 #endif