1 #pragma once 2 3 #include "types.hpp" 4 5 #include <stdexcept> 6 7 namespace vpd 8 { 9 /** @class Exception 10 * @brief This class inherits std::runtime_error and overrrides "what" method 11 * to return the description of exception. 12 * This class also works as base class for custom exception classes for 13 * VPD repository. 14 */ 15 class Exception : public std::runtime_error 16 { 17 public: 18 // deleted methods 19 Exception() = delete; 20 Exception(const Exception&) = delete; 21 Exception(Exception&&) = delete; 22 Exception& operator=(const Exception&) = delete; 23 24 // default destructor 25 ~Exception() = default; 26 27 /** @brief constructor 28 * 29 * @param[in] msg - Information w.r.t exception. 30 */ Exception(const std::string & msg)31 explicit Exception(const std::string& msg) : 32 std::runtime_error(msg), m_errMsg(msg) 33 {} 34 35 /** @brief inline method to return exception string. 36 * 37 * This is overridden method of std::runtime class. 38 */ what() const39 inline const char* what() const noexcept override 40 { 41 return m_errMsg.c_str(); 42 } 43 44 // TODO: Create getErrorType api by defining VPD default error type 45 46 private: 47 /** @brief string to hold the reason of exception */ 48 std::string m_errMsg; 49 50 }; // class Exception 51 52 /** @class EccException 53 * 54 * @brief This class extends Exceptions class and define type for ECC related 55 * exception in VPD. 56 */ 57 class EccException : public Exception 58 { 59 public: 60 // deleted methods 61 EccException() = delete; 62 EccException(const EccException&) = delete; 63 EccException(EccException&&) = delete; 64 EccException& operator=(const EccException&) = delete; 65 66 // default destructor 67 ~EccException() = default; 68 69 /** @brief constructor 70 * 71 * @param[in] msg - Information w.r.t exception. 72 */ EccException(const std::string & msg)73 explicit EccException(const std::string& msg) : Exception(msg) {} 74 75 /** @brief Method to get error type 76 * 77 * @return Error type which has to be logged for errors of type 78 * EccException. 79 */ getErrorType() const80 types::ErrorType getErrorType() const 81 { 82 return types::ErrorType::EccCheckFailed; 83 } 84 85 }; // class EccException 86 87 /** @class DataException 88 * 89 * @brief This class extends Exceptions class and define type for data related 90 * exception in VPD 91 */ 92 class DataException : public Exception 93 { 94 public: 95 // deleted methods 96 DataException() = delete; 97 DataException(const DataException&) = delete; 98 DataException(DataException&&) = delete; 99 DataException& operator=(const DataException&) = delete; 100 101 // default destructor 102 ~DataException() = default; 103 104 /** @brief constructor 105 * 106 * @param[in] msg - string to define exception 107 */ DataException(const std::string & msg)108 explicit DataException(const std::string& msg) : Exception(msg) {} 109 110 /** @brief Method to get error type 111 * 112 * @return Error type which has to be logged for errors of type 113 * DataException. 114 */ getErrorType() const115 types::ErrorType getErrorType() const 116 { 117 return types::ErrorType::InvalidVpdMessage; 118 } 119 }; // class DataException 120 121 class JsonException : public Exception 122 { 123 public: 124 // deleted methods 125 JsonException() = delete; 126 JsonException(const JsonException&) = delete; 127 JsonException(JsonException&&) = delete; 128 JsonException& operator=(const JsonException&) = delete; 129 130 // default destructor 131 ~JsonException() = default; 132 133 /** @brief constructor 134 * @param[in] msg - Information w.r.t. exception. 135 * @param[in] path - Json path 136 */ JsonException(const std::string & msg,const std::string & path)137 JsonException(const std::string& msg, const std::string& path) : 138 Exception(msg), m_jsonPath(path) 139 {} 140 141 /** @brief Json path getter method. 142 * 143 * @return - Json path 144 */ getJsonPath() const145 inline std::string getJsonPath() const 146 { 147 return m_jsonPath; 148 } 149 150 /** @brief Method to get error type 151 * 152 * @return Error type which has to be logged for errors of type 153 * JsonException. 154 */ getErrorType() const155 types::ErrorType getErrorType() const 156 { 157 return types::ErrorType::JsonFailure; 158 } 159 160 private: 161 /** To hold the path of Json that failed*/ 162 std::string m_jsonPath; 163 164 }; // class JSonException 165 166 /** @class GpioException 167 * @brief Custom handler for GPIO exception. 168 * 169 * This class extends Exceptions class and define 170 * type for GPIO related exception in VPD. 171 */ 172 class GpioException : public Exception 173 { 174 public: 175 // deleted methods 176 GpioException() = delete; 177 GpioException(const GpioException&) = delete; 178 GpioException(GpioException&&) = delete; 179 GpioException& operator=(const GpioException&) = delete; 180 181 // default destructor 182 ~GpioException() = default; 183 184 /** @brief constructor 185 * @param[in] msg - string to define exception 186 */ GpioException(const std::string & msg)187 explicit GpioException(const std::string& msg) : Exception(msg) {} 188 189 /** @brief Method to get error type 190 * 191 * @return Error type which has to be logged for errors of type 192 * GpioException. 193 */ getErrorType() const194 types::ErrorType getErrorType() const 195 { 196 return types::ErrorType::GpioError; 197 } 198 }; 199 200 /** @class DbusException 201 * @brief Custom handler for Dbus exception. 202 * 203 * This class extends Exceptions class and define 204 * type for DBus related exception in VPD. 205 */ 206 class DbusException : public Exception 207 { 208 public: 209 // deleted methods 210 DbusException() = delete; 211 DbusException(const DbusException&) = delete; 212 DbusException(DbusException&&) = delete; 213 DbusException& operator=(const DbusException&) = delete; 214 215 // default destructor 216 ~DbusException() = default; 217 218 /** @brief constructor 219 * @param[in] msg - string to define exception 220 */ DbusException(const std::string & msg)221 explicit DbusException(const std::string& msg) : Exception(msg) {} 222 223 /** @brief Method to get error type 224 * 225 * @return Error type which has to be logged for errors of type 226 * DbusException. 227 */ getErrorType() const228 types::ErrorType getErrorType() const 229 { 230 return types::ErrorType::DbusFailure; 231 } 232 }; 233 234 /** @class FirmwareException 235 * @brief Custom handler for firmware exception. 236 * 237 * This class extends Exceptions class and define 238 * type for generic firmware related exception in VPD. 239 */ 240 class FirmwareException : public Exception 241 { 242 public: 243 // deleted methods 244 FirmwareException() = delete; 245 FirmwareException(const FirmwareException&) = delete; 246 FirmwareException(FirmwareException&&) = delete; 247 FirmwareException& operator=(const FirmwareException&) = delete; 248 249 // default destructor 250 ~FirmwareException() = default; 251 252 /** @brief constructor 253 * @param[in] msg - string to define exception 254 */ FirmwareException(const std::string & msg)255 explicit FirmwareException(const std::string& msg) : Exception(msg) {} 256 257 /** @brief Method to get error type 258 * 259 * @return Error type which has to be logged for errors of type 260 * FirmwareException. 261 */ getErrorType() const262 types::ErrorType getErrorType() const 263 { 264 return types::ErrorType::InternalFailure; 265 } 266 }; 267 268 /** @class EepromException 269 * @brief Custom handler for EEPROM exception. 270 * 271 * This class extends Exceptions class and define 272 * type for EEPROM related exception in VPD. 273 */ 274 class EepromException : public Exception 275 { 276 public: 277 // deleted methods 278 EepromException() = delete; 279 EepromException(const EepromException&) = delete; 280 EepromException(EepromException&&) = delete; 281 EepromException& operator=(const EepromException&) = delete; 282 283 // default destructor 284 ~EepromException() = default; 285 286 /** @brief constructor 287 * @param[in] msg - string to define exception 288 */ EepromException(const std::string & msg)289 explicit EepromException(const std::string& msg) : Exception(msg) {} 290 291 /** @brief Method to get error type 292 * 293 * @return Error type which has to be logged for errors of type 294 * EepromException. 295 */ getErrorType() const296 types::ErrorType getErrorType() const 297 { 298 return types::ErrorType::InvalidEeprom; 299 } 300 }; 301 302 } // namespace vpd 303