68#ifndef snicholls_core_adapters_nanobind_errors_hpp
69#define snicholls_core_adapters_nanobind_errors_hpp
71#include <nanobind/nanobind.h>
73#include <snicholls/core/result.hpp>
74#include <snicholls/core/traced.hpp>
83namespace snicholls::python {
103 : std::runtime_error(
report.message), report_(std::move(
report))
108 [[nodiscard]]
const error_report&
report() const noexcept {
return report_; }
111 error_report report_;
116[[noreturn]]
inline void throw_precondition(
const char* message) {
throw precondition_error(message); }
120inline PyObject* error_class(
const char* name, PyObject* fallback)
noexcept
122 PyObject*
module = PyImport_ImportModule("snicholls_core.errors");
123 if (module ==
nullptr) {
127 PyObject* cls = PyObject_GetAttrString(module, name);
129 if (cls ==
nullptr) {
136inline PyObject* precondition_class() noexcept
138 static PyObject* cls = error_class(
"PreconditionError", PyExc_ValueError);
142inline PyObject* result_class() noexcept
144 static PyObject* cls = error_class(
"ResultError", PyExc_RuntimeError);
148inline PyObject* traced_class() noexcept
150 static PyObject* cls = error_class(
"TracedError", PyExc_RuntimeError);
154inline bool set_attr(PyObject* target,
const char* name, PyObject* value)
noexcept
156 if (value ==
nullptr)
158 const int status = PyObject_SetAttrString(target, name, value);
163inline PyObject* text(
const std::string& s)
noexcept
165 return PyUnicode_DecodeUTF8(s.data(),
static_cast<Py_ssize_t
>(s.size()),
"replace");
169inline void set_traced(
const error_report& r)
noexcept
171 PyObject* cls = traced_class();
172 PyObject* message = text(r.message);
173 if (message ==
nullptr)
175 PyObject* instance = PyObject_CallFunctionObjArgs(cls, message,
nullptr);
177 if (instance ==
nullptr)
179 bool ok = set_attr(instance,
"error", text(r.error)) && set_attr(instance,
"file", text(r.where.file_name())) &&
180 set_attr(instance,
"line", PyLong_FromLong(
static_cast<long>(r.where.line()))) &&
181 set_attr(instance,
"function", text(r.where.function_name())) &&
182 set_attr(instance,
"stack", text(r.stack));
184 PyObject* frames = PyList_New(0);
185 for (std::size_t i = 0; ok && frames !=
nullptr && i < r.frames.size(); ++i) {
186 PyObject* f = text(r.frames[i].message);
187 ok = f !=
nullptr && PyList_Append(frames, f) == 0;
190 ok = ok && set_attr(instance,
"frames", frames);
193 PyObject* fields = PyDict_New();
194 for (std::size_t i = 0; ok && fields !=
nullptr && i < r.fields.size(); ++i) {
195 PyObject* v = text(r.fields[i].value);
196 ok = v !=
nullptr && PyDict_SetItemString(fields, r.fields[i].key.c_str(), v) == 0;
199 ok = ok && set_attr(instance,
"fields", fields);
205 PyErr_SetObject(cls, instance);
210inline void translate_core(
const std::exception_ptr& p,
void*)
213 std::rethrow_exception(p);
214 }
catch (
const traced_exception& e) {
215 set_traced(e.report());
216 }
catch (
const precondition_error& e) {
217 PyErr_SetString(precondition_class(), e.what());
218 }
catch (
const bad_result_access<void>& e) {
219 PyErr_SetString(result_class(), e.what());
236inline void require(
bool condition,
const char* message)
238 if (!condition) [[unlikely]]
239 detail::throw_precondition(message);
254 using T = std::remove_cvref_t<E>;
255 if constexpr (is_traced_v<T>)
257 else if constexpr (std::is_base_of_v<std::exception, T> && std::is_copy_constructible_v<T>)
260 throw bad_result_access<void>(error_text(e));
270template <
class V,
class E>
273 if (r.is_err()) [[unlikely]]
275 if constexpr (std::is_void_v<V>)
278 return V(std::move(r).unwrap_unchecked());
290 detail::translate_core(p,
nullptr);
291 }
catch (::nanobind::python_error& e) {
293 }
catch (
const std::bad_alloc&) {
295 }
catch (
const std::out_of_range& e) {
296 PyErr_SetString(PyExc_IndexError, e.what());
297 }
catch (
const std::overflow_error& e) {
298 PyErr_SetString(PyExc_OverflowError, e.what());
299 }
catch (
const std::invalid_argument& e) {
300 PyErr_SetString(PyExc_ValueError, e.what());
301 }
catch (
const std::domain_error& e) {
302 PyErr_SetString(PyExc_ValueError, e.what());
303 }
catch (
const std::length_error& e) {
304 PyErr_SetString(PyExc_ValueError, e.what());
305 }
catch (
const std::exception& e) {
306 PyErr_SetString(PyExc_RuntimeError, e.what());
308 PyErr_SetString(PyExc_RuntimeError,
"unknown C++ exception");
311 PyErr_SetString(PyExc_RuntimeError,
"snicholls::python: an exception while translating an exception");
A violated precondition of a bound function: snicholls_core.errors.PreconditionError (a ValueError).
precondition_error(const std::string &what)
Constructs it.
A snicholls::traced<E> error on its way to Python: snicholls_core.errors.TracedError.
traced_exception(error_report report)
Constructs it from a report.
const error_report & report() const noexcept
The report.
decltype(auto) unwrap(result< V, E > &&r)
The value of a result, or raise_error of its error.
void register_exception_translators()
Registers the translators for core's errors with nanobind. Call once in each NB_MODULE body.
void set_python_error(const std::exception_ptr &p) noexcept
Sets the Python error for a C++ exception, by the file's table. For type casters, which return a null...
void raise_error(const E &e)
Throws e as the C++ exception that maps to the right Python class.
void require(bool condition, const char *message)
Throws precondition_error(message) unless condition holds.