1 /*------------------------------------------------------------------------------
  2 Name:      XmlBlasterException.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Basic xmlBlaster exception.
  6 ------------------------------------------------------------------------------*/
  7 
  8 /**
  9  * The basic exception handling class for xmlBlaster.
 10  * <p>
 11  * The getMessage() method returns a configurable formatted std::string
 12  * (TODO)
 13  * here is an example how to configure the format in your xmlBlaster.properties:
 14  * <pre>
 15  *  XmlBlasterException.logFormat=XmlBlasterException errorCode=[{0}] node=[{1}] location=[{2}] message=[{4} : {8}]
 16  *  XmlBlasterException.logFormat.internal= XmlBlasterException errorCode=[{0}] node=[{1}] location=[{2}]\nmessage={4} : {8}\nversionInfo={5}\nstackTrace={7}
 17  *  XmlBlasterException.logFormat.resource= defaults to XmlBlasterException.logFormat
 18  *  XmlBlasterException.logFormat.communication= defaults to XmlBlasterException.logFormat
 19  *  XmlBlasterException.logFormat.user= defaults to XmlBlasterException.logFormat
 20  *  XmlBlasterException.logFormat.transaction= defaults to XmlBlasterException.logFormat
 21  *  XmlBlasterException.logFormat.legacy= defaults to XmlBlasterException.logFormat
 22  * </pre>
 23  * where the replacements are:
 24  * <pre>
 25  *  {0} = errorCodeStr
 26  *  {1} = node
 27  *  {2} = location
 28  *  {3} = lang
 29  *  {4} = message
 30  *  {5} = versionInfo
 31  *  {6} = timestamp
 32  *  {7} = stackTrace
 33  *  {8} = embeddedMessage
 34  *  {9} = transactionInfo
 35  * </pre>
 36  * @author "Marcel Ruff" <xmlBlaster@marcelruff.info>
 37  * @author "Michele Laghi" <laghi@swissinfo.org>
 38  * @since 0.8+ with extended attributes
 39  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/admin.errorcodes.html">The admin.errorcodes requirement</a>
 40  */
 41 
 42 #ifndef _UTIL_XMLBLASTER_EXCEPTION_H
 43 #define _UTIL_XMLBLASTER_EXCEPTION_H
 44 
 45 #include <string>
 46 // #include <util/Timestamp.h>
 47 #include <util/ErrorCode.h>
 48 #include <stdexcept>
 49 
 50 namespace org { namespace xmlBlaster { namespace util {
 51 
 52 class Dll_Export XmlBlasterException : public std::exception
 53 {
 54    private:
 55       const std::string errorCodeStr_;
 56       const std::string node_;
 57       std::string location_;
 58       const std::string lang_;
 59       const std::string message_;
 60       const std::string versionInfo_;
 61       mutable std::string timestamp_;
 62       std::string stackTrace_;
 63       std::string embeddedMessage_;
 64       const std::string transactionInfo_;
 65       mutable std::string str_; // for what(), holds memory for const char * return
 66 
 67    public:
 68 
 69    /**
 70     * For internal use: Deserializing and exception creation from CORBA XmlBlasterException
 71     */
 72    XmlBlasterException(const std::string &errorCodeStr,
 73                        const std::string &node,
 74                        const std::string &location,
 75                        const std::string &lang="en",
 76                        const std::string &message="",
 77                        const std::string &versionInfo="@version@",  // is replaced by ant / build.xml to e.g. "0.85c", see org::xmlBlaster::util::Global.getVersion(),
 78 //                       org::xmlBlaster::util::Timestamp timestamp=0,
 79                        const std::string &timestampStr="",
 80                        const std::string &stackTrace="",
 81                        const std::string &embeddedMessage="",
 82                        const std::string &transactionInfo="<transactioninfo/>");
 83 
 84    XmlBlasterException(const ErrorCode &errorCode,
 85                        const std::string &node,
 86                        const std::string &location,
 87                        const std::string &lang,
 88                        const std::string &versionInfo="@version@",  // is replaced by ant / build.xml to e.g. "0.85c", see org::xmlBlaster::util::Global.getVersion(),
 89 //                       org::xmlBlaster::util::Timestamp timestamp=0,
 90                        const std::string &timestampStr="",
 91                        const std::string &stackTrace="",
 92                        const std::string &embeddedMessage="",
 93                        const std::string &transactionInfo="<transactioninfo/>");
 94 
 95    XmlBlasterException(const ErrorCode &errorCode,
 96                        const std::string &node,
 97                        const std::string &embeddedMessage);
 98 
 99    virtual ~XmlBlasterException() throw();
100 
101    std::string getErrorCodeStr() const;
102    std::string getNode() const;
103    void setLocation(const std::string& location) { this->location_ = location; }
104    std::string getLocation() const;
105    std::string getLang() const;
106    std::string getMessage() const;
107 
108    /**
109     * @return The original message text
110     */
111    std::string getRawMessage() const;
112 
113    /**
114     * A comma separated list with key/values containing detailed
115     * information about the server environment
116     */
117    std::string getVersionInfo() const;
118 
119    /**
120     * org::xmlBlaster::util::Timestamp when exception was thrown
121     */
122    std::string getTimestamp() const;
123 
124    /**
125     * @return The stack trace or null, e.g.
126     * <pre>
127     *  stackTrace= errorCode=internal.unknown message=Bla bla
128     *    at org.xmlBlaster.util.XmlBlasterException.main(XmlBlasterException.java:488)
129     * </pre>
130     * The first line is the result from toString() and the following lines
131     * are the stackTrace
132     */
133    std::string getStackTraceStr() const;
134 
135    /**
136     * @return The toString() of the embedded exception which is <classname>:getMessage()<br />
137     *         or null if not applicable
138     */
139    std::string getEmbeddedMessage() const;
140 
141    /**
142     * @return Not defined yet
143     */
144    std::string getTransactionInfo() const;
145 
146    bool isInternal() const;
147 
148    bool isResource() const;
149 
150    bool isCommunication() const;
151 
152    bool isUser() const;
153 
154    bool isTransaction() const;
155 
156    /**
157     * Enforced by std::exception
158     * @return The complete exception text with location and message
159     */ 
160    const char *what() const throw();
161 
162    /**
163     * Returns a std::stringified version of the exception
164     */
165    std::string toString() const;
166 
167    /**
168     * Parsing what toString() produced
169     */
170    static XmlBlasterException parseFromString(std::string fromString);
171 
172    /**
173     * Create a XML representation of the Exception.
174     * <pre>
175     *   &lt;exception errorCode='resource.outOfMemory'>
176     *      &lt;class>JavaClass&lt;/class>
177     *      &lt;message>&lt;![cdata[  bla bla ]]>&lt;/message>
178     *   &lt;/exception>
179     * </pre>
180     */
181    std::string toXml() const;
182 
183    /**
184     * Returns a std::string containing the stack trace if the system and the
185     * compilation permit it (_ENABLE_STACK_TRACE must be set: which is set
186     * in xmlBlasterDef.h in case of the gnu compiler.
187     */
188    static std::string getStackTrace(int maxNumOfLines=20);
189 
190 };
191 
192 }}} // namespaces
193 
194 #endif


syntax highlighted by Code2HTML, v. 0.9.1