-
Notifications
You must be signed in to change notification settings - Fork 0
radex Exceptions #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
radex Exceptions #38
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
931c120
basic exceptions
ashao 4de873e
Make header modifications discoverable on rebuild
ashao 779b0b0
Wire in exceptions up to Python client
ashao 7d2f2a9
Update src/python/setup.py
ashao 1af7e82
Move import up
ashao d997176
Revert "Move import up"
ashao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #ifndef __RADEX_EXCEPTIONS_HPP__ | ||
| #define __RADEX_EXCEPTIONS_HPP__ | ||
|
|
||
| #include <stdexcept> | ||
|
|
||
| namespace radex { | ||
|
|
||
| /// Base class for every error raised by radex. | ||
| class Error : public std::runtime_error { | ||
| public: | ||
| using std::runtime_error::runtime_error; | ||
| }; | ||
|
|
||
| /// A key was requested that is not present in the store. | ||
| class KeyNotFoundError : public Error { | ||
| public: | ||
| using Error::Error; | ||
| }; | ||
|
|
||
| /// A key did not appear in the store before timing out. | ||
| class TimeoutError : public Error { | ||
| public: | ||
| using Error::Error; | ||
| }; | ||
|
|
||
| /// The stored item does not match the item type requested. | ||
| class TypeMismatchError : public Error { | ||
| public: | ||
| using Error::Error; | ||
| }; | ||
|
|
||
| /// A scalar was requested at a tensor key, or a tensor at a scalar key. | ||
| class RankMismatchError : public TypeMismatchError { | ||
| public: | ||
| using TypeMismatchError::TypeMismatchError; | ||
| }; | ||
|
|
||
| /// The stored element type differs from the requested one. Distinct from | ||
| /// `RankMismatchError` so that callers can retry with another element type | ||
| /// without also retrying a request that asked for the wrong shape entirely. | ||
| class DTypeMismatchError : public TypeMismatchError { | ||
| public: | ||
| using TypeMismatchError::TypeMismatchError; | ||
| }; | ||
|
|
||
| /// An item's metadata record could not be decoded. | ||
| class MetadataError : public Error { | ||
| public: | ||
| using Error::Error; | ||
| }; | ||
|
|
||
| /// A client was requested for a backend that was disabled at build time. | ||
| class BackendUnavailableError : public Error { | ||
| public: | ||
| using Error::Error; | ||
| }; | ||
|
|
||
| } // namespace radex | ||
|
|
||
| #endif // __RADEX_EXCEPTIONS_HPP__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| from libc.time cimport timespec | ||
|
|
||
| from radex.clients.core cimport IClient | ||
| from radex.utils.exceptions cimport raise_py_error | ||
|
|
||
|
|
||
| cdef extern from "radex/dragon.hpp" namespace "radex::drg::ddict": | ||
| cdef cppclass Client(IClient): | ||
| Client() except + | ||
| Client(const char*, const timespec*) except + | ||
| Client() except +raise_py_error | ||
| Client(const char*, const timespec*) except +raise_py_error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| from libcpp.string_view cimport string_view | ||
|
|
||
| from radex.clients.core cimport IClient | ||
| from radex.utils.exceptions cimport raise_py_error | ||
|
|
||
|
|
||
| cdef extern from "radex/smartredis.hpp" namespace "radex::redis::smartredis": | ||
| cdef cppclass Client(IClient): | ||
| Client() except + | ||
| Client(string_view) except + | ||
| Client() except +raise_py_error | ||
| Client(string_view) except +raise_py_error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Python mirror of the C++ exception hierarchy in ``radex/exceptions.hpp``. | ||
| """ | ||
|
|
||
| __all__ = [ | ||
| "RadexError", | ||
| "KeyNotFoundError", | ||
| "TimeoutError", | ||
| "TypeMismatchError", | ||
| "RankMismatchError", | ||
| "DTypeMismatchError", | ||
| "MetadataError", | ||
| "BackendUnavailableError", | ||
| ] | ||
|
|
||
|
|
||
| class RadexError(RuntimeError): | ||
| """Base class for every error raised by radex.""" | ||
|
|
||
|
|
||
| class KeyNotFoundError(RadexError): | ||
| """A key was requested that is not present in the store.""" | ||
|
|
||
|
|
||
| class TimeoutError(RadexError): | ||
| """A key did not appear in the store before the timeout elapsed.""" | ||
|
|
||
|
|
||
| class TypeMismatchError(RadexError): | ||
| """The stored item does not match the description it was requested with.""" | ||
|
|
||
|
|
||
| class RankMismatchError(TypeMismatchError): | ||
| """A scalar was requested at a tensor key, or a tensor at a scalar key.""" | ||
|
|
||
|
|
||
| class DTypeMismatchError(TypeMismatchError): | ||
| """The stored element type differs from the requested one.""" | ||
|
|
||
|
|
||
| class MetadataError(RadexError): | ||
| """An item's metadata record could not be decoded.""" | ||
|
|
||
|
|
||
| class BackendUnavailableError(RadexError): | ||
| """A client was requested for a backend that was disabled at build time.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #ifndef __RADEX_PY_EXCEPTION_TRANSLATION_HPP__ | ||
| #define __RADEX_PY_EXCEPTION_TRANSLATION_HPP__ | ||
|
|
||
| #include <Python.h> | ||
|
|
||
| #include <new> | ||
| #include <stdexcept> | ||
|
|
||
| #include "radex/exceptions.hpp" | ||
|
|
||
| namespace radex_py { | ||
|
|
||
| namespace detail { | ||
|
|
||
| inline void set_error(const char *name, const char *what) { | ||
| PyObject *module = PyImport_ImportModule("radex.exceptions"); | ||
| if (module == nullptr) { | ||
| return; // ImportError is already set | ||
| } | ||
|
|
||
| PyObject *exc_type = PyObject_GetAttrString(module, name); | ||
| if (exc_type != nullptr) { | ||
| PyErr_SetString(exc_type, what); | ||
| Py_DECREF(exc_type); | ||
| } | ||
|
|
||
| Py_DECREF(module); | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| /// Translate the in-flight C++ exception into the matching Python one. | ||
| /// | ||
| /// Only valid inside a catch block, which is where Cython's | ||
| /// `except +raise_py_error` invokes it. Derived types must be caught before | ||
| /// their bases or the subclass information is lost. | ||
| inline void raise_py_error() { | ||
| try { | ||
| throw; | ||
| } catch (const radex::KeyNotFoundError &e) { | ||
| detail::set_error("KeyNotFoundError", e.what()); | ||
| } catch (const radex::TimeoutError &e) { | ||
| detail::set_error("TimeoutError", e.what()); | ||
| } catch (const radex::RankMismatchError &e) { | ||
| detail::set_error("RankMismatchError", e.what()); | ||
| } catch (const radex::DTypeMismatchError &e) { | ||
| detail::set_error("DTypeMismatchError", e.what()); | ||
| } catch (const radex::TypeMismatchError &e) { | ||
| detail::set_error("TypeMismatchError", e.what()); | ||
| } catch (const radex::MetadataError &e) { | ||
| detail::set_error("MetadataError", e.what()); | ||
| } catch (const radex::BackendUnavailableError &e) { | ||
| detail::set_error("BackendUnavailableError", e.what()); | ||
| } catch (const radex::Error &e) { | ||
| detail::set_error("RadexError", 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::bad_alloc &e) { | ||
| PyErr_SetString(PyExc_MemoryError, e.what()); | ||
| } catch (const std::exception &e) { | ||
| PyErr_SetString(PyExc_RuntimeError, e.what()); | ||
| } catch (...) { | ||
| PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception"); | ||
| } | ||
| } | ||
|
|
||
| } // namespace radex_py | ||
|
|
||
| #endif // __RADEX_PY_EXCEPTION_TRANSLATION_HPP__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| cdef extern from "radex/utils/exception_translation.hpp" namespace "radex_py": | ||
| void raise_py_error() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might be able to move this and the import at line 73 into a top level
as I don't think that
radex.exceptionsuses anything from this module and therefor we do not need to guard against the circular import.That said, I'm not entirely sure how will this will play nice with the Cython compile step. It might be worth a shot, but if it doesn't work immediately feel free to ignore this comment.