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 WriteVerificationError
26  *
27  * An error that occurred when performing a read after a write to ensure the
28  * write was successful.
29  *
30  * This exception is thrown when the value read is different than the value
31  * written.  This is also known as a readback error.
32  */
33 class WriteVerificationError : public std::exception
34 {
35   public:
36     // Specify which compiler-generated methods we want
37     WriteVerificationError() = delete;
38     WriteVerificationError(const WriteVerificationError&) = default;
39     WriteVerificationError(WriteVerificationError&&) = default;
40     WriteVerificationError& operator=(const WriteVerificationError&) = default;
41     WriteVerificationError& operator=(WriteVerificationError&&) = default;
42     virtual ~WriteVerificationError() = default;
43 
44     /**
45      * Constructor.
46      *
47      * @param error error message
48      * @param deviceID unique ID of the device where error occurred
49      * @param inventoryPath inventory path of the device where error occurred
50      */
51     explicit WriteVerificationError(const std::string& error,
52                                     const std::string& deviceID,
53                                     const std::string& inventoryPath) :
54         error{"WriteVerificationError: " + error},
55         deviceID{deviceID}, inventoryPath{inventoryPath}
56     {}
57 
58     /**
59      * Returns the unique ID of the device where the error occurred.
60      *
61      * @return device ID
62      */
63     const std::string& getDeviceID() const
64     {
65         return deviceID;
66     }
67 
68     /**
69      * Returns the inventory path of the device where the error occurred.
70      *
71      * @return inventory path
72      */
73     const std::string& getInventoryPath() const
74     {
75         return inventoryPath;
76     }
77 
78     /**
79      * Returns the description of this error.
80      *
81      * @return error description
82      */
83     const char* what() const noexcept override
84     {
85         return error.c_str();
86     }
87 
88   private:
89     /**
90      * Error message.
91      */
92     const std::string error{};
93 
94     /**
95      * Unique ID of the device where the error occurred.
96      */
97     const std::string deviceID{};
98 
99     /**
100      * Inventory path of the device where the error occurred.
101      */
102     const std::string inventoryPath{};
103 };
104 
105 } // namespace phosphor::power::regulators
106