blob: b17db3d4696a53b6744dabf052a7c3b82f3f5f12 [file] [log] [blame]
Patrick Venture123b5c02019-03-05 14:01:00 -08001#pragma once
2
3#include <exception>
4#include <map>
Patrick Venture123b5c02019-03-05 14:01:00 -08005#include <string>
6
Patrick Venture1470bec2019-03-06 07:33:12 -08007namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -08008{
9
10class IpmiException : public std::exception
11{
12 public:
Patrick Ventureef5e0922019-05-29 09:33:49 -070013 IpmiException(const std::string& message, int code) :
14 _message(message), _ccode(code){};
Patrick Venture123b5c02019-03-05 14:01:00 -080015
Patrick Venture0ba794d2019-03-15 21:07:09 -070016 static std::string messageFromIpmi(int cc)
Patrick Venture123b5c02019-03-05 14:01:00 -080017 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070018 const std::map<int, std::string> commonFailures = {
19 {0xc0, "busy"},
20 {0xc1, "invalid"},
21 {0xc3, "timeout"},
22 };
Patrick Venture123b5c02019-03-05 14:01:00 -080023
24 auto search = commonFailures.find(cc);
25 if (search != commonFailures.end())
26 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070027 return "Received IPMI_CC: " + search->second;
Patrick Venture123b5c02019-03-05 14:01:00 -080028 }
29 else
30 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070031 return "Received IPMI_CC: " + std::to_string(cc);
Patrick Venture123b5c02019-03-05 14:01:00 -080032 }
Patrick Venture123b5c02019-03-05 14:01:00 -080033 }
Patrick Venture0ba794d2019-03-15 21:07:09 -070034
Patrick Ventureef5e0922019-05-29 09:33:49 -070035 /* This will leave the code as 0, but by virtue of being an exception
36 * thrown, we'll know it was an error on the host-side.
37 */
Patrick Williamsc99b19f2023-05-10 07:51:10 -050038 explicit IpmiException(const std::string& message) : _message(message) {}
Patrick Ventureef5e0922019-05-29 09:33:49 -070039
Patrick Williamsc99b19f2023-05-10 07:51:10 -050040 explicit IpmiException(int cc) : IpmiException(messageFromIpmi(cc), cc) {}
Patrick Venture123b5c02019-03-05 14:01:00 -080041
42 virtual const char* what() const noexcept override
43 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070044 return _message.c_str();
Patrick Venture123b5c02019-03-05 14:01:00 -080045 }
46
Patrick Ventureef5e0922019-05-29 09:33:49 -070047 int code() const noexcept
48 {
49 return _ccode;
50 }
51
Patrick Venture123b5c02019-03-05 14:01:00 -080052 private:
Patrick Venture0ba794d2019-03-15 21:07:09 -070053 std::string _message;
Patrick Ventureef5e0922019-05-29 09:33:49 -070054 int _ccode = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080055};
56
Patrick Venture1470bec2019-03-06 07:33:12 -080057} // namespace ipmiblob