-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexception.cpp
More file actions
56 lines (48 loc) · 1.91 KB
/
exception.cpp
File metadata and controls
56 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// libcachesim_python - libCacheSim Python bindings
// Copyright 2025 The libcachesim Authors. All rights reserved.
//
// Use of this source code is governed by a GPL-3.0
// license that can be found in the LICENSE file or at
// https://github.com/1a1a11a/libcachesim/blob/develop/LICENSE
#include "exception.h"
#include <pybind11/pybind11.h>
namespace libcachesim {
namespace py = pybind11;
void register_exception(py::module& m) {
static py::exception<CacheException> exc_cache(m, "CacheException");
static py::exception<ReaderException> exc_reader(m, "ReaderException");
py::register_exception_translator([](std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
} catch (const CacheException& e) {
exc_cache(e.what());
} catch (const ReaderException& e) {
exc_reader(e.what());
}
});
py::register_exception_translator([](std::exception_ptr p) {
try {
if (p) std::rethrow_exception(p);
} catch (const std::bad_alloc& e) {
PyErr_SetString(PyExc_MemoryError, e.what());
} catch (const std::invalid_argument& e) {
PyErr_SetString(PyExc_ValueError, e.what());
} catch (const std::out_of_range& e) {
PyErr_SetString(PyExc_IndexError, e.what());
} catch (const std::domain_error& e) {
PyErr_SetString(PyExc_ValueError,
("Domain error: " + std::string(e.what())).c_str());
} catch (const std::overflow_error& e) {
PyErr_SetString(PyExc_OverflowError, e.what());
} catch (const std::range_error& e) {
PyErr_SetString(PyExc_ValueError,
("Range error: " + std::string(e.what())).c_str());
} catch (const std::runtime_error& e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
} catch (const std::exception& e) {
PyErr_SetString(PyExc_RuntimeError,
("C++ exception: " + std::string(e.what())).c_str());
}
});
}
} // namespace libcachesim