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      */
PMBusError(const std::string & error,const std::string & deviceID,const std::string & inventoryPath)48     explicit PMBusError(const std::string& error, const std::string& deviceID,
49                         const std::string& inventoryPath) :
50         error{"PMBusError: " + error}, deviceID{deviceID},
51         inventoryPath{inventoryPath}
52     {}
53 
54     /**
55      * Returns the unique ID of the device where the error occurred.
56      *
57      * @return device ID
58      */
getDeviceID() const59     const std::string& getDeviceID() const
60     {
61         return deviceID;
62     }
63 
64     /**
65      * Returns the inventory path of the device where the error occurred.
66      *
67      * @return inventory path
68      */
getInventoryPath() const69     const std::string& getInventoryPath() const
70     {
71         return inventoryPath;
72     }
73 
74     /**
75      * Returns the description of this error.
76      *
77      * @return error description
78      */
what() const79     const char* what() const noexcept override
80     {
81         return error.c_str();
82     }
83 
84   private:
85     /**
86      * Error message.
87      */
88     const std::string error{};
89 
90     /**
91      * Unique ID of the device where the error occurred.
92      */
93     const std::string deviceID{};
94 
95     /**
96      * Inventory path of the device where the error occurred.
97      */
98     const std::string inventoryPath{};
99 };
100 
101 } // namespace phosphor::power::regulators
102