aboutsummaryrefslogtreecommitdiff
path: root/libs/plugin_python/src/runner/error.h
blob: 341c7ea3a0e8fdb1f90e17474c46193658cffac2 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef ERROR_H
#define ERROR_H

#include <format>

#include <QString>

#include <pybind11/pybind11.h>

#include <uibase/utility.h>

namespace pyexcept {

    /**
     * @brief Exception to throw when a python implementation does not implement
     *     a pure virtual function.
     */
    class MissingImplementation : public MOBase::Exception {
    public:
        MissingImplementation(std::string const& className,
                              std::string const& methodName)
            : Exception(QString::fromStdString(
                  std::format("Python class implementing \"{}\" has no "
                              "implementation of method \"{}\".",
                              className, methodName)))
        {
        }
    };

    /**
     * @brief Exception to throw when a python error occurs.
     */
    class PythonError : public MOBase::Exception {
    public:
        /**
         * @brief Create a new PythonError, fetching the error message from
         * python. If the message cannot be retrieved, `defaultErrorMessage()`
         * is used instead.
         */
        PythonError(pybind11::error_already_set const& ex) : Exception(ex.what()) {}

        /**
         * @brief Create a new PythonError with the given message.
         *
         * @param message Message for the exception.
         */
        PythonError(QString message) : Exception(message) {}
    };

    /**
     * @brief Exception to throw when an unknown error occured. This is
     * typically thrown from a catch(...) block.
     */
    class UnknownException : public MOBase::Exception {
    public:
        /**
         * @brief Create a new UnknownException with the default message.
         *
         * @see defaultErrorMessage
         */
        UnknownException() : Exception(defaultErrorMessage()) {}

        /**
         * @brief Create a new UnknownException with the given message.
         *
         * @param message Message for the exception.
         */
        UnknownException(QString message) : Exception(message) {}

    protected:
        /**
         *
         */
        static QString defaultErrorMessage()
        {
            return QObject::tr("An unknown exception was thrown in python code.");
        }
    };

}  // namespace pyexcept

#endif  // ERROR_H