Exceptions.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // <Exceptions.hpp> -*- C++ -*- | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cassert> | ||
| 6 | #include <exception> | ||
| 7 | #include <sstream> | ||
| 8 | #include <string> | ||
| 9 | |||
| 10 | namespace simdb { | ||
| 11 | |||
| 12 | /*! | ||
| 13 | * \class DBException | ||
| 14 | * | ||
| 15 | * \brief SimDB exception type; stream additional context with operator<< before | ||
| 16 | * throwing. what() returns the full message built from the initial | ||
| 17 | * reason and any appended values. | ||
| 18 | */ | ||
| 19 | class DBException : public std::exception | ||
| 20 | { | ||
| 21 | public: | ||
| 22 | DBException() = default; | ||
| 23 | |||
| 24 | /// \brief Construct with an initial reason string. | ||
| 25 | 38060 | explicit DBException(const std::string_view reason) { reason_ << reason; } | |
| 26 | |||
| 27 | /// \brief Copy constructor; copies the accumulated message. | ||
| 28 | 35 | DBException(const DBException& rhs) { reason_ << rhs.reason_.str(); } | |
| 29 | |||
| 30 | 38095 | virtual ~DBException() noexcept override {} | |
| 31 | |||
| 32 | /// \brief Override from std::exception; returns the full exception message. | ||
| 33 | 1 | virtual const char* what() const noexcept override | |
| 34 | { | ||
| 35 | 1 | reason_str_ = reason_.str(); | |
| 36 | 1 | return reason_str_.c_str(); | |
| 37 | } | ||
| 38 | |||
| 39 | /// \brief Append a value to the exception message (stream-style). | ||
| 40 | 222 | template <typename T> DBException& operator<<(const T& msg) | |
| 41 | { | ||
| 42 | 222 | reason_ << msg; | |
| 43 | 222 | return *this; | |
| 44 | } | ||
| 45 | |||
| 46 | private: | ||
| 47 | // The reason/explanation for the exception | ||
| 48 | std::stringstream reason_; | ||
| 49 | |||
| 50 | // Need to keep a local copy of the string formed in the | ||
| 51 | // string stream for the 'what' call | ||
| 52 | mutable std::string reason_str_; | ||
| 53 | }; | ||
| 54 | |||
| 55 | /*! | ||
| 56 | * \class SafeTransactionSilentException | ||
| 57 | * | ||
| 58 | * \brief Internal exception signaling that safeTransaction() should retry (e.g. | ||
| 59 | * SQLITE_BUSY, SQLITE_LOCKED). Not propagated out of safeTransaction(); | ||
| 60 | * the transaction is retried instead. | ||
| 61 | */ | ||
| 62 | class SafeTransactionSilentException : public std::exception | ||
| 63 | { | ||
| 64 | public: | ||
| 65 | /// \brief Construct with the SQLite return code (used in the message). | ||
| 66 | 1 | explicit SafeTransactionSilentException(int rc) : | |
| 67 | 1 | msg_("The database is locked (return code " + std::to_string(rc) + ")") | |
| 68 | { | ||
| 69 | 1 | } | |
| 70 | |||
| 71 | ✗ | const char* what() const noexcept override { return msg_.c_str(); } | |
| 72 | |||
| 73 | private: | ||
| 74 | const std::string msg_; | ||
| 75 | }; | ||
| 76 | |||
| 77 | /*! | ||
| 78 | * \class InterruptException | ||
| 79 | * | ||
| 80 | * \brief This exception is used in order to break out of | ||
| 81 | * the worker thread's infinite consumer loop. | ||
| 82 | */ | ||
| 83 | class InterruptException : public std::exception | ||
| 84 | { | ||
| 85 | public: | ||
| 86 | const char* what() const noexcept override { return "Infinite consumer loop has been interrupted"; } | ||
| 87 | |||
| 88 | private: | ||
| 89 | /// Private constructor. Not to be created by anyone but the | ||
| 90 | /// WorkerInterrupt. | ||
| 91 | InterruptException() = default; | ||
| 92 | friend class WorkerInterrupt; | ||
| 93 | }; | ||
| 94 | |||
| 95 | } // namespace simdb | ||
| 96 |