jexcept.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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);
  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);
  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. #define makeUnexpectedException() makeStringExceptionV(9999, "Internal Error at %s(%d)", __FILE__, __LINE__)
  96. #define throwUnexpected() throw makeStringExceptionV(9999, "Internal Error at %s(%d)", __FILE__, __LINE__)
  97. #define throwUnexpectedX(x) throw makeStringExceptionV(9999, "Internal Error '" x "' at %s(%d)", __FILE__, __LINE__)
  98. #define assertThrow(x) assertex(x)
  99. #define UNIMPLEMENTED throw makeStringExceptionV(-1, "UNIMPLEMENTED feature at %s(%d)", __FILE__, __LINE__)
  100. #define UNIMPLEMENTED_X(reason) throw makeStringExceptionV(-1, "UNIMPLEMENTED '" reason "' at %s(%d)", __FILE__, __LINE__)
  101. #define UNIMPLEMENTED_XY(a,b) throw makeStringExceptionV(-1, "UNIMPLEMENTED " a " %s at %s(%d)", b, __FILE__, __LINE__)
  102. IException jlib_decl * deserializeException(MemoryBuffer & in);
  103. void jlib_decl serializeException(IException * e, MemoryBuffer & out);
  104. void jlib_decl printStackReport();
  105. // Macro for legacy name of above function
  106. #define PrintStackReport printStackReport
  107. #ifdef _DEBUG
  108. #define RELEASE_CATCH_ALL int*********
  109. #else
  110. #define RELEASE_CATCH_ALL ...
  111. #endif
  112. //These are used in several places to wrap error reporting, to keep error numbers+text together. E.g.,
  113. //#define XYZfail 99 #define XXZfail_Text "Failed" throwError(XYZfail)
  114. #define throwError(x) throwStringExceptionV(x, (x ## _Text))
  115. #define throwError1(x,a) throwStringExceptionV(x, (x ## _Text), a)
  116. #define throwError2(x,a,b) throwStringExceptionV(x, (x ## _Text), a, b)
  117. #define throwError3(x,a,b,c) throwStringExceptionV(x, (x ## _Text), a, b, c)
  118. #define throwError4(x,a,b,c,d) throwStringExceptionV(x, (x ## _Text), a, b, c, d)
  119. #endif