@@ -26,11 +26,11 @@ void bind_exceptions(py::module &m) {
26
26
27
27
m.def (" severity_to_string" , &SeverityToString, " Convert ErrorSeverity to string" );
28
28
29
- // Bind BaseException
30
- py::exception <BaseException>(m, " BaseException" )
29
+ // Bind BaseException as regular class
30
+ py::class_ <BaseException>(m, " BaseException" )
31
31
.def (py::init<std::string_view, ErrorSeverity>(), py::arg (" message" ),
32
32
py::arg (" severity" ) = ErrorSeverity::kError )
33
- .def (" what" , & BaseException:: what)
33
+ .def (" what" , []( const BaseException &e) { return std::string (e. what ()); } )
34
34
.def (" get_severity" , &BaseException::GetSeverity)
35
35
.def (" get_location" , &BaseException::GetLocation, py::return_value_policy::reference_internal)
36
36
.def (" get_formatted_message" , &BaseException::GetFormattedMessage)
@@ -40,7 +40,7 @@ void bind_exceptions(py::module &m) {
40
40
});
41
41
42
42
// Bind ValidationException
43
- py::exception <ValidationException>(m, " ValidationException" , m. attr ( " BaseException " ) )
43
+ py::class_ <ValidationException, BaseException >(m, " ValidationException" )
44
44
.def (py::init<std::string_view, std::optional<std::string_view>>(), py::arg (" message" ),
45
45
py::arg (" field_name" ) = std::nullopt)
46
46
.def (" get_field_name" , &ValidationException::GetFieldName)
@@ -52,7 +52,7 @@ void bind_exceptions(py::module &m) {
52
52
});
53
53
54
54
// Bind ResourceException
55
- py::exception <ResourceException>(m, " ResourceException" , m. attr ( " BaseException " ) )
55
+ py::class_ <ResourceException, BaseException >(m, " ResourceException" )
56
56
.def (py::init<std::string_view, std::optional<std::string_view>>(), py::arg (" message" ),
57
57
py::arg (" resource_name" ) = std::nullopt)
58
58
.def (" get_resource_name" , &ResourceException::GetResourceName)
@@ -64,7 +64,7 @@ void bind_exceptions(py::module &m) {
64
64
});
65
65
66
66
// Bind CalculationException
67
- py::exception <CalculationException>(m, " CalculationException" , m. attr ( " BaseException " ) )
67
+ py::class_ <CalculationException, BaseException >(m, " CalculationException" )
68
68
.def (py::init<std::string_view, double >(), py::arg (" message" ), py::arg (" input_value" ) = 0.0 )
69
69
.def (" get_input_value" , &CalculationException::GetInputValue)
70
70
.def (" __repr__" , [](const CalculationException &e) {
@@ -77,16 +77,19 @@ void bind_exceptions(py::module &m) {
77
77
.def (py::init<int >())
78
78
.def (py::init<BaseException>())
79
79
.def (" has_value" , &Result<int >::HasValue)
80
- .def (" get_value" , &Result<int >::GetValue, py::return_value_policy::reference_internal)
81
- .def (" get_exception" , &Result<int >::GetException, py::return_value_policy::reference_internal)
82
- .def (" visit" , &Result<int >::Visit<std::function<py::object (const auto &)>>)
80
+ .def (" get_value" ,
81
+ [](const Result<int > &r) -> int {
82
+ return r.GetValue (); // This will throw if error, which pybind11 will convert to
83
+ // Python exception
84
+ })
85
+ .def (" get_exception" , [](const Result<int > &r) -> BaseException { return r.GetException (); })
83
86
.def (" map" , &Result<int >::Map<std::function<int (const int &)>>)
84
87
.def (" then" , &Result<int >::Then<std::function<Result<int >(const int &)>>)
85
88
.def (" __bool__" , &Result<int >::HasValue)
86
89
.def (" __str__" ,
87
90
[](const Result<int > &r) {
88
91
return r.HasValue () ? std::format (" Result({})" , r.GetValue ())
89
- : std::format (" Result(Error: {})" , r.GetException ().GetMessage ());
92
+ : std::format (" Result(Error: {})" , r.GetException ().what ());
90
93
})
91
94
.def (" __repr__" , [](const Result<int > &r) {
92
95
return std::format (" <IntResult({}) at {}>" ,
@@ -99,17 +102,16 @@ void bind_exceptions(py::module &m) {
99
102
.def (py::init<double >())
100
103
.def (py::init<BaseException>())
101
104
.def (" has_value" , &Result<double >::HasValue)
102
- .def (" get_value" , &Result<double >::GetValue, py::return_value_policy::reference_internal)
103
- .def (" get_exception" , &Result<double >::GetException,
104
- py::return_value_policy::reference_internal)
105
- .def (" visit" , &Result<double >::Visit<std::function<py::object (const auto &)>>)
105
+ .def (" get_value" , [](const Result<double > &r) -> double { return r.GetValue (); })
106
+ .def (" get_exception" ,
107
+ [](const Result<double > &r) -> BaseException { return r.GetException (); })
106
108
.def (" map" , &Result<double >::Map<std::function<double (const double &)>>)
107
109
.def (" then" , &Result<double >::Then<std::function<Result<double >(const double &)>>)
108
110
.def (" __bool__" , &Result<double >::HasValue)
109
111
.def (" __str__" ,
110
112
[](const Result<double > &r) {
111
113
return r.HasValue () ? std::format (" Result({})" , r.GetValue ())
112
- : std::format (" Result(Error: {})" , r.GetException ().GetMessage ());
114
+ : std::format (" Result(Error: {})" , r.GetException ().what ());
113
115
})
114
116
.def (" __repr__" , [](const Result<double > &r) {
115
117
return std::format (" <DoubleResult({}) at {}>" ,
@@ -122,18 +124,17 @@ void bind_exceptions(py::module &m) {
122
124
.def (py::init<std::string>())
123
125
.def (py::init<BaseException>())
124
126
.def (" has_value" , &Result<std::string>::HasValue)
125
- .def (" get_value" , &Result<std::string>::GetValue, py::return_value_policy::reference_internal)
126
- .def (" get_exception" , &Result<std::string>::GetException,
127
- py::return_value_policy::reference_internal)
128
- .def (" visit" , &Result<std::string>::Visit<std::function<py::object (const auto &)>>)
127
+ .def (" get_value" , [](const Result<std::string> &r) -> std::string { return r.GetValue (); })
128
+ .def (" get_exception" ,
129
+ [](const Result<std::string> &r) -> BaseException { return r.GetException (); })
129
130
.def (" map" , &Result<std::string>::Map<std::function<std::string (const std::string &)>>)
130
131
.def (" then" ,
131
132
&Result<std::string>::Then<std::function<Result<std::string>(const std::string &)>>)
132
133
.def (" __bool__" , &Result<std::string>::HasValue)
133
134
.def (" __str__" ,
134
135
[](const Result<std::string> &r) {
135
136
return r.HasValue () ? std::format (" Result('{}')" , r.GetValue ())
136
- : std::format (" Result(Error: {})" , r.GetException ().GetMessage ());
137
+ : std::format (" Result(Error: {})" , r.GetException ().what ());
137
138
})
138
139
.def (" __repr__" , [](const Result<std::string> &r) {
139
140
return std::format (" <StringResult({}) at {}>" ,
0 commit comments