ComIn 0.5.1
ICON Community Interface
Loading...
Searching...
No Matches
exception.h
Go to the documentation of this file.
1#ifndef EXCPEPTION_H
2#define EXCPEPTION_H
3
4#include <memory>
5#include <stdexcept>
6#include <string>
7
8#define PY_SSIZE_T_CLEAN
9#include <Python.h>
10#include <comin.h>
11
12namespace comin::python {
13
14extern PyObject* PyExc_ComInError;
15
16class ComInError : public std::runtime_error {
17 std::string _what;
18
19 static std::string format_message(t_comin_error_code error_code,
20 std::string add_info) {
21 return std::string(comin_error_get_category(error_code)) + ": " +
22 std::string(comin_error_get_string(error_code)) + "\n" + add_info;
23 }
24
25public:
26 ComInError(t_comin_error_code error_code, std::string add_info)
27 : std::runtime_error(format_message(error_code, add_info)) {}
28};
29
30void check_error(std::string add_info = "");
31
32template <PyCFunction Fun>
33PyObject* func_wrapper(PyObject* self, PyObject* args) {
34 try {
35 return Fun(self, args);
36 } catch (ComInError& e) {
37 return PyErr_Format(PyExc_ComInError, e.what());
38 }
39}
40
41template <PyCFunctionWithKeywords Fun>
42PyObject* func_wrapper(PyObject* self, PyObject* args, PyObject* kwargs) {
43 try {
44 return Fun(self, args, kwargs);
45 } catch (ComInError& e) {
46 return PyErr_Format(PyExc_ComInError, e.what());
47 }
48}
49} // namespace comin::python
50#endif
ComInError(t_comin_error_code error_code, std::string add_info)
Definition exception.h:26
C interface for the ICON Community Interface.
const char * comin_error_get_category(t_comin_error_code error_code)
const char * comin_error_get_string(t_comin_error_code error_code)
void check_error(std::string add_info)
Definition exception.cpp:7
PyObject * PyExc_ComInError
Definition exception.cpp:5
PyObject * func_wrapper(PyObject *self, PyObject *args)
Definition exception.h:33