blob: 355debd83362075b1dd8d1b3d210535df3b47163 [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 */
38 explicit IpmiException(const std::string& message) : _message(message)
Patrick Venture60edc612020-06-24 15:06:41 -070039 {}
Patrick Ventureef5e0922019-05-29 09:33:49 -070040
41 explicit IpmiException(int cc) : IpmiException(messageFromIpmi(cc), cc)
Patrick Venture60edc612020-06-24 15:06:41 -070042 {}
Patrick Venture123b5c02019-03-05 14:01:00 -080043
44 virtual const char* what() const noexcept override
45 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070046 return _message.c_str();
Patrick Venture123b5c02019-03-05 14:01:00 -080047 }
48
Patrick Ventureef5e0922019-05-29 09:33:49 -070049 int code() const noexcept
50 {
51 return _ccode;
52 }
53
Patrick Venture123b5c02019-03-05 14:01:00 -080054 private:
Patrick Venture0ba794d2019-03-15 21:07:09 -070055 std::string _message;
Patrick Ventureef5e0922019-05-29 09:33:49 -070056 int _ccode = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080057};
58
Patrick Venture1470bec2019-03-06 07:33:12 -080059} // namespace ipmiblob