| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #ifndef _JP_EXCEPTION_H_ |
| 17 | #define _JP_EXCEPTION_H_ |
| 18 | |
| 19 | /* All exception are passed as JPypeException. The type of the exception |
| 20 | * is specified at creation. Exceptions may be of type |
| 21 | * - _java_error - exception generated from within java. |
| 22 | * - _python_error - exception generated from within python. |
| 23 | * - _runtime_error - Failure that will issue a runtime error in python and java. |
| 24 | * - _type_error - Failure that will issue a type error in python. |
| 25 | * |
| 26 | * We must throw the correct exception so that it can properly be handled |
| 27 | * when returning to the native code. |
| 28 | * |
| 29 | * If we are returning to python, and it is a |
| 30 | * - _python_error, then we assume that a python exception has already been |
| 31 | * placed in the python virtual machine. |
| 32 | * - _java_error, then we will convert it to a python object with the correct |
| 33 | * object type. |
| 34 | * - otherwise, then we will convert it to the requested python error. |
| 35 | * |
| 36 | * If we are returning to java, and it is a |
| 37 | * - _java_error, then we assume there is already a Java exception queue |
| 38 | * in the virtual machine. |
| 39 | * - otherwise convert to a RuntimeException. |
| 40 | * |
| 41 | */ |
| 42 | #include <stdexcept> |
| 43 | #ifndef __FUNCTION_NAME__ |
| 44 | #ifdef WIN32 //WINDOWS |
| 45 | #define __FUNCTION_NAME__ __FUNCTION__ |
| 46 | #else //*NIX |
| 47 | #define __FUNCTION_NAME__ __func__ |
| 48 | #endif |
| 49 | #endif |
| 50 | |
| 51 | /** |
| 52 | * This is the type of the exception to issue. |
| 53 | */ |
| 54 | enum JPError |
| 55 | { |
| 56 | _java_error, |
| 57 | _python_error, |
| 58 | _python_exc, |
| 59 | _os_error_unix, |
| 60 | _os_error_windows, |
| 61 | }; |
| 62 | |
| 63 | // Create a stackinfo for a particular location in the code that can then |
| 64 | // be passed to the handler routine for auditing. |
| 65 | #define JP_STACKINFO() JPStackInfo(__FUNCTION_NAME__, __FILE__, __LINE__) |
| 66 | |
| 67 | |
| 68 | |
| 69 | // Macro to use when hardening code |
| 70 | // Most of these will be removed after core is debugged, but |
| 71 | // a few are necessary to handle off normal conditions. |
| 72 | #define ASSERT_NOT_NULL(X) {if ((X)==NULL) { JP_RAISE(PyExc_RuntimeError, "Null Pointer Exception");} } |
| 73 | |
| 74 | // Macro to add stack trace info when multiple paths lead to the same trouble spot |
| 75 | #define JP_CATCH catch (JPypeException& ex) { ex.from(JP_STACKINFO()); throw; } |
| 76 | |
| 77 | /** Structure to pass around the location within a C++ source file. |
| 78 | */ |
| 79 | class JPStackInfo |
| 80 | { |
| 81 | const char* function_; |
| 82 | const char* file_; |
| 83 | int line_; |
| 84 | public: |
| 85 | |
| 86 | JPStackInfo(const char* function, const char* file, int line) |
| 87 | : function_(function), file_(file), line_(line) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | const char* getFunction() const |
| 92 | { |
| 93 | return function_; |
| 94 | } |
| 95 | |
| 96 | const char* getFile() const |
| 97 | { |
| 98 | return file_; |
| 99 | } |
| 100 | |
| 101 | int getLine() const |
| 102 | { |
| 103 | return line_; |
| 104 | } |
| 105 | } ; |
| 106 | using JPStackTrace = vector<JPStackInfo>; |
| 107 | |
| 108 | typedef union |
| 109 | { |
| 110 | int i; |
| 111 | void* l; |
| 112 | } JPErrorUnion; |
| 113 | |
| 114 | /** |
| 115 | * Exception issued by JPype to indicate an internal problem. |
| 116 | * |
| 117 | * This is primarily focused on transferring exception handling |
| 118 | * to Python as the majority of errors are reported there. |
| 119 | * |
| 120 | */ |
| 121 | class JPypeException : std::runtime_error |
| 122 | { |
| 123 | public: |
| 124 | JPypeException(JPJavaFrame &frame, jthrowable, const JPStackInfo& stackInfo); |
| 125 | JPypeException(int type, void* error, const JPStackInfo& stackInfo); |
| 126 | JPypeException(int type, void* error, const string& msn, const JPStackInfo& stackInfo); |
| 127 | JPypeException(int type, const string& msn, int error, const JPStackInfo& stackInfo); |
| 128 | // The copy constructor for an object thrown as an exception must be declared noexcept, including any implicitly-defined copy constructors. |
| 129 | // Any function declared noexcept that terminates by throwing an exception violates ERR55-CPP. Honor exception specifications. |
| 130 | JPypeException(const JPypeException &ex) noexcept; |
| 131 | JPypeException& operator = (const JPypeException& ex); |
| 132 | ~JPypeException() override = default; |
| 133 | |
| 134 | void from(const JPStackInfo& info); |
| 135 | |
| 136 | void convertJavaToPython(); |
| 137 | void convertPythonToJava(); |
| 138 | |
| 139 | /** Transfer handling of this exception to python. |
| 140 | * |
| 141 | * This should appear in the catch block whenever we return to python. |
| 142 | * |
| 143 | */ |
| 144 | void toPython(); |
| 145 | |
| 146 | /** Transfer handling of this exception to java. */ |
| 147 | void toJava(); |
| 148 | |
| 149 | int getExceptionType() const |
| 150 | { |
| 151 | return m_Type; |
| 152 | } |
| 153 | |
| 154 | jthrowable getThrowable() |
| 155 | { |
| 156 | return m_Throwable.get(); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | int m_Type; |
| 161 | JPErrorUnion m_Error{}; |
| 162 | JPStackTrace m_Trace; |
| 163 | JPThrowableRef m_Throwable; |
| 164 | std::string m_Message; |
| 165 | }; |
| 166 | |
| 167 | #endif |