xref: /openbmc/openpower-vpd-parser/vpd-manager/include/exceptions.hpp (revision 0a5fce1e84d0d742073c8a5bbd565a91eaa688af)
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 constructor
142      *  @param[in] msg - Information w.r.t. exception.
143      */
JsonException(const std::string & msg)144     JsonException(const std::string& msg) : Exception(msg) {}
145 
146     /** @brief Json path getter method.
147      *
148      *  @return - Json path
149      */
getJsonPath() const150     inline std::string getJsonPath() const
151     {
152         return m_jsonPath;
153     }
154 
155     /** @brief Method to get error type
156      *
157      * @return Error type which has to be logged for errors of type
158      * JsonException.
159      */
getErrorType() const160     types::ErrorType getErrorType() const
161     {
162         return types::ErrorType::JsonFailure;
163     }
164 
165   private:
166     /** To hold the path of Json that failed*/
167     std::string m_jsonPath;
168 
169 }; // class JSonException
170 
171 /** @class GpioException
172  *  @brief Custom handler for GPIO exception.
173  *
174  *  This class extends Exceptions class and define
175  *  type for GPIO related exception in VPD.
176  */
177 class GpioException : public Exception
178 {
179   public:
180     // deleted methods
181     GpioException() = delete;
182     GpioException(const GpioException&) = delete;
183     GpioException(GpioException&&) = delete;
184     GpioException& operator=(const GpioException&) = delete;
185 
186     // default destructor
187     ~GpioException() = default;
188 
189     /** @brief constructor
190      *  @param[in] msg - string to define exception
191      */
GpioException(const std::string & msg)192     explicit GpioException(const std::string& msg) : Exception(msg) {}
193 
194     /** @brief Method to get error type
195      *
196      * @return Error type which has to be logged for errors of type
197      * GpioException.
198      */
getErrorType() const199     types::ErrorType getErrorType() const
200     {
201         return types::ErrorType::GpioError;
202     }
203 };
204 
205 /** @class DbusException
206  *  @brief Custom handler for Dbus exception.
207  *
208  *  This class extends Exceptions class and define
209  *  type for DBus related exception in VPD.
210  */
211 class DbusException : public Exception
212 {
213   public:
214     // deleted methods
215     DbusException() = delete;
216     DbusException(const DbusException&) = delete;
217     DbusException(DbusException&&) = delete;
218     DbusException& operator=(const DbusException&) = delete;
219 
220     // default destructor
221     ~DbusException() = default;
222 
223     /** @brief constructor
224      *  @param[in] msg - string to define exception
225      */
DbusException(const std::string & msg)226     explicit DbusException(const std::string& msg) : Exception(msg) {}
227 
228     /** @brief Method to get error type
229      *
230      * @return Error type which has to be logged for errors of type
231      * DbusException.
232      */
getErrorType() const233     types::ErrorType getErrorType() const
234     {
235         return types::ErrorType::DbusFailure;
236     }
237 };
238 
239 /** @class FirmwareException
240  *  @brief Custom handler for firmware exception.
241  *
242  *  This class extends Exceptions class and define
243  *  type for generic firmware related exception in VPD.
244  */
245 class FirmwareException : public Exception
246 {
247   public:
248     // deleted methods
249     FirmwareException() = delete;
250     FirmwareException(const FirmwareException&) = delete;
251     FirmwareException(FirmwareException&&) = delete;
252     FirmwareException& operator=(const FirmwareException&) = delete;
253 
254     // default destructor
255     ~FirmwareException() = default;
256 
257     /** @brief constructor
258      *  @param[in] msg - string to define exception
259      */
FirmwareException(const std::string & msg)260     explicit FirmwareException(const std::string& msg) : Exception(msg) {}
261 
262     /** @brief Method to get error type
263      *
264      * @return Error type which has to be logged for errors of type
265      * FirmwareException.
266      */
getErrorType() const267     types::ErrorType getErrorType() const
268     {
269         return types::ErrorType::InternalFailure;
270     }
271 };
272 
273 /** @class EepromException
274  *  @brief Custom handler for EEPROM exception.
275  *
276  *  This class extends Exceptions class and define
277  *  type for EEPROM related exception in VPD.
278  */
279 class EepromException : public Exception
280 {
281   public:
282     // deleted methods
283     EepromException() = delete;
284     EepromException(const EepromException&) = delete;
285     EepromException(EepromException&&) = delete;
286     EepromException& operator=(const EepromException&) = delete;
287 
288     // default destructor
289     ~EepromException() = default;
290 
291     /** @brief constructor
292      *  @param[in] msg - string to define exception
293      */
EepromException(const std::string & msg)294     explicit EepromException(const std::string& msg) : Exception(msg) {}
295 
296     /** @brief Method to get error type
297      *
298      * @return Error type which has to be logged for errors of type
299      * EepromException.
300      */
getErrorType() const301     types::ErrorType getErrorType() const
302     {
303         return types::ErrorType::InvalidEeprom;
304     }
305 };
306 
307 } // namespace vpd
308