blob: 1050af36611edd51ca3c4a0a4aae4ea944820bae [file] [log] [blame]
Shawn McCarneya5ef5402020-03-01 16:29:44 -06001/**
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
21namespace 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 */
30class 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
Shawn McCarney5b819f42021-03-16 14:41:15 -050045 * @param deviceID unique ID of the device where error occurred
46 * @param inventoryPath inventory path of the device where error occurred
Shawn McCarneya5ef5402020-03-01 16:29:44 -060047 */
Shawn McCarney5b819f42021-03-16 14:41:15 -050048 explicit PMBusError(const std::string& error, const std::string& deviceID,
49 const std::string& inventoryPath) :
50 error{"PMBusError: " + error},
51 deviceID{deviceID}, inventoryPath{inventoryPath}
Shawn McCarneya5ef5402020-03-01 16:29:44 -060052 {
53 }
54
55 /**
Shawn McCarney5b819f42021-03-16 14:41:15 -050056 * 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 /**
Shawn McCarneya5ef5402020-03-01 16:29:44 -060076 * 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{};
Shawn McCarney5b819f42021-03-16 14:41:15 -050090
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{};
Shawn McCarneya5ef5402020-03-01 16:29:44 -0600100};
101
102} // namespace phosphor::power::regulators