jexcept.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef __JEXCEPT__
  14. #define __JEXCEPT__
  15. #include "jiface.hpp"
  16. #include "jlib.hpp"
  17. #include "errno.h"
  18. jlib_decl const char* serializeMessageAudience(MessageAudience ma);
  19. jlib_decl MessageAudience deserializeMessageAudience(const char* text);
  20. //the following interface to be thrown when a user command explicitly calls for a failure
  21. interface jlib_thrown_decl IUserException : public IException
  22. {
  23. };
  24. //the following interface defines a collection of exceptions
  25. //
  26. interface jlib_thrown_decl IMultiException : extends IException
  27. {
  28. //convenience methods for handling this as an array
  29. virtual aindex_t ordinality() const = 0;
  30. virtual IException& item(aindex_t pos) const = 0;
  31. virtual const char* source() const = 0;
  32. //for complete control...
  33. virtual IArrayOf<IException>& getArray()= 0;
  34. //add another exception
  35. virtual void append(IException& e) = 0;
  36. virtual void append(IMultiException& e) = 0;
  37. virtual StringBuffer& serialize(StringBuffer& ret, unsigned indent = 0, bool simplified=false, bool root=true) const = 0;
  38. virtual void deserialize(const char* xml) = 0; //throws IException on failure!
  39. //the following methods override those in IIException
  40. //
  41. virtual int errorCode() const = 0;
  42. virtual StringBuffer& errorMessage(StringBuffer &msg) const = 0;
  43. virtual MessageAudience errorAudience() const = 0;
  44. };
  45. IMultiException jlib_decl *makeMultiException(const char* source = NULL);
  46. interface IExceptionHandler
  47. {
  48. virtual bool fireException(IException *e) = 0;
  49. };
  50. IException jlib_decl *makeStringExceptionV(int code, const char *why, ...) __attribute__((format(printf, 2, 3)));
  51. IException jlib_decl *makeStringExceptionVA(int code, const char *why, va_list args) __attribute__((format(printf, 2, 0)));
  52. IException jlib_decl *makeStringException(int code, const char *why);
  53. IException jlib_decl *makeStringExceptionV(MessageAudience aud, int code, const char *why, ...) __attribute__((format(printf, 3, 4)));
  54. IException jlib_decl *makeStringExceptionVA(MessageAudience aud, int code, const char *why, va_list args) __attribute__((format(printf, 3, 0)));
  55. IException jlib_decl *makeStringException(MessageAudience aud, int code, const char *why);
  56. __declspec(noreturn) void jlib_decl throwStringExceptionV(int code, const char *format, ...) __attribute__((format(printf, 2, 3), noreturn));
  57. // Macros for legacy names of above functions
  58. #define MakeMultiException makeMultiException
  59. #define MakeStringException makeStringExceptionV
  60. #define MakeStringExceptionVA makeStringExceptionVA
  61. #define MakeStringExceptionDirect makeStringException
  62. #define ThrowStringException throwStringExceptionV
  63. interface jlib_thrown_decl IOSException: extends IException{};
  64. IOSException jlib_decl *makeOsException(int code);
  65. IOSException jlib_decl *makeOsException(int code, const char *msg);
  66. IOSException jlib_decl *makeOsExceptionV(int code, const char *msg, ...) __attribute__((format(printf, 2, 3)));
  67. #define DISK_FULL_EXCEPTION_CODE ENOSPC
  68. interface jlib_thrown_decl IErrnoException: extends IException{};
  69. IErrnoException jlib_decl *makeErrnoException(int errn, const char *why);
  70. IErrnoException jlib_decl *makeErrnoException(const char *why);
  71. IErrnoException jlib_decl *makeErrnoExceptionV(int errn, const char *why, ...) __attribute__((format(printf, 2, 3)));
  72. IErrnoException jlib_decl *makeErrnoExceptionV(const char *why, ...) __attribute__((format(printf, 1, 2)));
  73. IErrnoException jlib_decl *makeErrnoException(MessageAudience aud, int errn, const char *why);
  74. IErrnoException jlib_decl *makeErrnoExceptionV(MessageAudience aud, int errn, const char *why, ...) __attribute__((format(printf, 3, 4)));
  75. IErrnoException jlib_decl *makeErrnoExceptionV(MessageAudience aud, const char *why, ...) __attribute__((format(printf, 2, 3)));
  76. void jlib_decl pexception(const char *msg,IException *e); // like perror except for exceptions
  77. jlib_decl StringBuffer & formatSystemError(StringBuffer & out, unsigned errcode);
  78. void userBreakpoint();
  79. interface jlib_thrown_decl ISEH_Exception : extends IException
  80. {
  81. };
  82. interface jlib_thrown_decl IOutOfMemException: extends IException
  83. {
  84. };
  85. void jlib_decl enableSEHtoExceptionMapping();
  86. void jlib_decl disableSEHtoExceptionMapping();
  87. // NB only enables for current thread or threads started after call
  88. // requires /EHa option to be set in VC++ options (after /GX)
  89. // Macros for legacy names of above functions
  90. #define EnableSEHtoExceptionMapping enableSEHtoExceptionMapping
  91. #define DisableSEHtoExceptionMapping disableSEHtoExceptionMapping
  92. void jlib_decl *setSEHtoExceptionHandler(IExceptionHandler *handler); // sets handler and return old value
  93. void jlib_decl setTerminateOnSEHInSystemDLLs(bool set=true);
  94. void jlib_decl setTerminateOnSEH(bool set=true);
  95. __declspec(noreturn) void jlib_decl throwUnexpectedException(const char * file, unsigned line) __attribute__((noreturn));
  96. __declspec(noreturn) void jlib_decl throwUnexpectedException(const char * where, const char * file, unsigned line) __attribute__((noreturn));
  97. const char jlib_decl *sanitizeSourceFile(const char *file);
  98. #define makeUnexpectedException() makeStringExceptionV(9999, "Internal Error at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__)
  99. #define throwUnexpected() throwUnexpectedException(sanitizeSourceFile(__FILE__), __LINE__)
  100. #define throwUnexpectedX(x) throwUnexpectedException(x, sanitizeSourceFile(__FILE__), __LINE__)
  101. #define assertThrow(x) assertex(x)
  102. #define UNIMPLEMENTED throw makeStringExceptionV(-1, "UNIMPLEMENTED feature at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__)
  103. #define UNIMPLEMENTED_X(reason) throw makeStringExceptionV(-1, "UNIMPLEMENTED '" reason "' at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__)
  104. #define UNIMPLEMENTED_XY(a,b) throw makeStringExceptionV(-1, "UNIMPLEMENTED " a " %s at %s(%d)", b, sanitizeSourceFile(__FILE__), __LINE__)
  105. IException jlib_decl * deserializeException(MemoryBuffer & in);
  106. void jlib_decl serializeException(IException * e, MemoryBuffer & out);
  107. void jlib_decl printStackReport(__int64 startIP = 0);
  108. // Macro for legacy name of above function
  109. #define PrintStackReport printStackReport
  110. #ifdef _DEBUG
  111. #define RELEASE_CATCH_ALL int*********
  112. #else
  113. #define RELEASE_CATCH_ALL ...
  114. #endif
  115. //These are used in several places to wrap error reporting, to keep error numbers+text together. E.g.,
  116. //#define XYZfail 99 #define XXZfail_Text "Failed" throwError(XYZfail)
  117. #define throwError(x) throwStringExceptionV(x, (x ## _Text))
  118. #define throwError1(x,a) throwStringExceptionV(x, (x ## _Text), a)
  119. #define throwError2(x,a,b) throwStringExceptionV(x, (x ## _Text), a, b)
  120. #define throwError3(x,a,b,c) throwStringExceptionV(x, (x ## _Text), a, b, c)
  121. #define throwError4(x,a,b,c,d) throwStringExceptionV(x, (x ## _Text), a, b, c, d)
  122. //---------------------------------------------------------------------------------------------------------------------
  123. //These values are persisted - so should not be reordered.
  124. enum ErrorSeverity
  125. {
  126. SeverityInformation = 0,
  127. SeverityWarning = 1,
  128. SeverityError = 2,
  129. SeverityAlert = 3,
  130. SeverityIgnore = 4,
  131. SeverityFatal = 5, // a fatal error - can't be mapped to anything else
  132. SeverityUnknown,
  133. SeverityMax = SeverityUnknown
  134. };
  135. inline bool isError(ErrorSeverity severity) { return severity == SeverityError || severity == SeverityFatal; }
  136. inline bool isFatal(ErrorSeverity severity) { return severity == SeverityFatal; }
  137. //TBD in a separate commit - add support for warnings to be associated with different categories
  138. enum WarnErrorCategory
  139. {
  140. CategoryInformation,// Some kind of information [default severity information]
  141. CategoryCast, // Suspicious casts between types or out of range values
  142. CategoryConfuse, // Likely to cause confusion
  143. CategoryDeprecated, // deprecated features or syntax
  144. CategoryEfficiency, // Something that is likely to be inefficient
  145. CategoryFolding, // Unusual results from constant folding
  146. CategoryFuture, // Likely to cause problems in future versions
  147. CategoryIgnored, // Something that has no effect, or is ignored
  148. CategoryIndex, // Unusual indexing of datasets or strings
  149. CategoryMistake, // Almost certainly a mistake
  150. CategoryLimit, // An operation that should really have some limits to protect data runaway
  151. CategorySyntax, // Invalid syntax which is painless to recover from
  152. CategoryUnusual, // Not strictly speaking an error, but highly unusual and likely to be a mistake
  153. CategoryUnexpected, // Code that could be correct, but has the potential for unexpected behaviour
  154. CategoryCpp, // Warning passed through from C++ compiler
  155. CategorySecurity, // Security warnings - operations that will be refused at codegen time unless unused.
  156. CategoryDFS, // DFS resolution issues - field translation may not have occurred even though requested
  157. CategoryError, // Typically severity fatal
  158. CategoryAll,
  159. CategoryUnknown,
  160. CategoryMax,
  161. };
  162. interface jlib_thrown_decl IError : public IException
  163. {
  164. public:
  165. virtual const char* getFilename() const = 0;
  166. virtual WarnErrorCategory getCategory() const = 0;
  167. virtual int getLine() const = 0;
  168. virtual int getColumn() const = 0;
  169. virtual int getPosition() const = 0;
  170. virtual StringBuffer& toString(StringBuffer&) const = 0;
  171. virtual ErrorSeverity getSeverity() const = 0;
  172. virtual IError * cloneSetSeverity(ErrorSeverity _severity) const = 0;
  173. virtual unsigned getActivity() const = 0;
  174. virtual const char * queryScope() const = 0;
  175. };
  176. inline bool isError(IError * error) { return isError(error->getSeverity()); }
  177. inline bool isFatal(IError * error) { return isFatal(error->getSeverity()); }
  178. extern jlib_decl IError *createError(WarnErrorCategory category, ErrorSeverity severity, int errNo, const char *msg, const char *filename, int lineno=0, int column=0, int pos=0, unsigned activity = 0, const char * scope = nullptr);
  179. extern jlib_decl IError *createError(WarnErrorCategory category, ErrorSeverity severity, int errNo, const char *msg, unsigned activity, const char * scope);
  180. #endif