Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <exception> |
| 4 | #include <map> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 7 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 8 | { |
| 9 | |
| 10 | class IpmiException : public std::exception |
| 11 | { |
| 12 | public: |
Patrick Venture | ef5e092 | 2019-05-29 09:33:49 -0700 | [diff] [blame] | 13 | IpmiException(const std::string& message, int code) : |
| 14 | _message(message), _ccode(code){}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 15 | |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 16 | static std::string messageFromIpmi(int cc) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 17 | { |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 18 | const std::map<int, std::string> commonFailures = { |
| 19 | {0xc0, "busy"}, |
| 20 | {0xc1, "invalid"}, |
| 21 | {0xc3, "timeout"}, |
| 22 | }; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 23 | |
| 24 | auto search = commonFailures.find(cc); |
| 25 | if (search != commonFailures.end()) |
| 26 | { |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 27 | return "Received IPMI_CC: " + search->second; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 28 | } |
| 29 | else |
| 30 | { |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 31 | return "Received IPMI_CC: " + std::to_string(cc); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 32 | } |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 33 | } |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 34 | |
Patrick Venture | ef5e092 | 2019-05-29 09:33:49 -0700 | [diff] [blame] | 35 | /* 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 Williams | c99b19f | 2023-05-10 07:51:10 -0500 | [diff] [blame] | 38 | explicit IpmiException(const std::string& message) : _message(message) {} |
Patrick Venture | ef5e092 | 2019-05-29 09:33:49 -0700 | [diff] [blame] | 39 | |
Patrick Williams | c99b19f | 2023-05-10 07:51:10 -0500 | [diff] [blame] | 40 | explicit IpmiException(int cc) : IpmiException(messageFromIpmi(cc), cc) {} |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 41 | |
| 42 | virtual const char* what() const noexcept override |
| 43 | { |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 44 | return _message.c_str(); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Patrick Venture | ef5e092 | 2019-05-29 09:33:49 -0700 | [diff] [blame] | 47 | int code() const noexcept |
| 48 | { |
| 49 | return _ccode; |
| 50 | } |
| 51 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 52 | private: |
Patrick Venture | 0ba794d | 2019-03-15 21:07:09 -0700 | [diff] [blame] | 53 | std::string _message; |
Patrick Venture | ef5e092 | 2019-05-29 09:33:49 -0700 | [diff] [blame] | 54 | int _ccode = 0; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 57 | } // namespace ipmiblob |