1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <exception>
19 #include <string>
20 
21 namespace phosphor::power::regulators
22 {
23 
24 /**
25  * @class PMBusError
26  *
27  * An error that occurred while sending PMBus commands or converting data
28  * between PMBus formats.
29  */
30 class PMBusError : public std::exception
31 {
32   public:
33     // Specify which compiler-generated methods we want
34     PMBusError() = delete;
35     PMBusError(const PMBusError&) = default;
36     PMBusError(PMBusError&&) = default;
37     PMBusError& operator=(const PMBusError&) = default;
38     PMBusError& operator=(PMBusError&&) = default;
39     virtual ~PMBusError() = default;
40 
41     /**
42      * Constructor.
43      *
44      * @param error error message
45      * @param deviceID unique ID of the device where error occurred
46      * @param inventoryPath inventory path of the device where error occurred
47      */
48     explicit PMBusError(const std::string& error, const std::string& deviceID,
49                         const std::string& inventoryPath) :
50         error{"PMBusError: " + error},
51         deviceID{deviceID}, inventoryPath{inventoryPath}
52     {
53     }
54 
55     /**
56      * Returns the unique ID of the device where the error occurred.
57      *
58      * @return device ID
59      */
60     const std::string& getDeviceID() const
61     {
62         return deviceID;
63     }
64 
65     /**
66      * Returns the inventory path of the device where the error occurred.
67      *
68      * @return inventory path
69      */
70     const std::string& getInventoryPath() const
71     {
72         return inventoryPath;
73     }
74 
75     /**
76      * Returns the description of this error.
77      *
78      * @return error description
79      */
80     const char* what() const noexcept override
81     {
82         return error.c_str();
83     }
84 
85   private:
86     /**
87      * Error message.
88      */
89     const std::string error{};
90 
91     /**
92      * Unique ID of the device where the error occurred.
93      */
94     const std::string deviceID{};
95 
96     /**
97      * Inventory path of the device where the error occurred.
98      */
99     const std::string inventoryPath{};
100 };
101 
102 } // namespace phosphor::power::regulators
103