blob: e41b3cf8f911e62142a71cdadbec605d326fc30e [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 Venture0ba794d2019-03-15 21:07:09 -070013 explicit IpmiException(const std::string& message) : _message(message){};
Patrick Venture123b5c02019-03-05 14:01:00 -080014
Patrick Venture0ba794d2019-03-15 21:07:09 -070015 static std::string messageFromIpmi(int cc)
Patrick Venture123b5c02019-03-05 14:01:00 -080016 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070017 const std::map<int, std::string> commonFailures = {
18 {0xc0, "busy"},
19 {0xc1, "invalid"},
20 {0xc3, "timeout"},
21 };
Patrick Venture123b5c02019-03-05 14:01:00 -080022
23 auto search = commonFailures.find(cc);
24 if (search != commonFailures.end())
25 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070026 return "Received IPMI_CC: " + search->second;
Patrick Venture123b5c02019-03-05 14:01:00 -080027 }
28 else
29 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070030 return "Received IPMI_CC: " + std::to_string(cc);
Patrick Venture123b5c02019-03-05 14:01:00 -080031 }
Patrick Venture123b5c02019-03-05 14:01:00 -080032 }
Patrick Venture0ba794d2019-03-15 21:07:09 -070033
34 explicit IpmiException(int cc) : IpmiException(messageFromIpmi(cc))
35 {
36 }
Patrick Venture123b5c02019-03-05 14:01:00 -080037
38 virtual const char* what() const noexcept override
39 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070040 return _message.c_str();
Patrick Venture123b5c02019-03-05 14:01:00 -080041 }
42
43 private:
Patrick Venture0ba794d2019-03-15 21:07:09 -070044 std::string _message;
Patrick Venture123b5c02019-03-05 14:01:00 -080045};
46
Patrick Venture1470bec2019-03-06 07:33:12 -080047} // namespace ipmiblob