Salome HOME
15bb8d6a01bfb841f8e440130de4134774bc0fc7
[tools/py2cpp.git] / src / Errors.hxx
1 // Copyright (C) 2019-2023 EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef PY2CPP_ERRORS_HXX
20 #define PY2CPP_ERRORS_HXX
21
22 #include "py2cppExports.hxx"
23
24 #include <string>
25 #include <exception>
26 #include <Python.h>
27
28 namespace py2cpp
29 {
30 /*!
31  * Retrieve the last python error and return it as a formated string, containing
32  * the traceback when possible.
33  * After the call of this function, the python error indicator is cleared.
34  * see PyErr_Fetch.
35  */
36 PY2CPP_EXPORT std::string getLastPyError();
37
38 /*!
39  * ConversionCheck class gathers the errors within fromPy functions.
40  */
41 class PY2CPP_EXPORT ConversionCheck
42 {
43 public:
44   ConversionCheck();
45   ConversionCheck(const std::string& error);
46   ~ConversionCheck();
47   operator bool()const; //! true means no error
48   void addError(const std::string& expectedType, PyObject * obj);
49   void addError(const ConversionCheck& err);
50   void addErrorMessage(const std::string& message);
51   void reset(); //! Empty the error message.
52   const std::string& getMessage()const;
53 private:
54   std::string _message;
55 };
56
57 class PY2CPP_EXPORT Exception:public std::exception
58 {
59 public:
60   Exception(const std::string& message);
61   virtual const char* what() const noexcept;
62 private:
63   std::string _message;
64 };
65
66 class PY2CPP_EXPORT ConversionException:public Exception
67 {
68 public:
69   ConversionException(const std::string& message);
70 };
71
72 class PY2CPP_EXPORT ExecutionException:public Exception
73 {
74 public:
75   ExecutionException(const std::string& message);
76 };
77
78 class PY2CPP_EXPORT AttributeException:public Exception
79 {
80 public:
81   AttributeException(const std::string& message);
82 };
83
84 }
85
86 #endif //PY2CPP_ERRORS_HXX