blob: 15a5d4c5183a33a0d0f0c3837febb79dbbbe8d4a [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)
39 {
40 }
41
42 explicit IpmiException(int cc) : IpmiException(messageFromIpmi(cc), cc)
Patrick Venture0ba794d2019-03-15 21:07:09 -070043 {
44 }
Patrick Venture123b5c02019-03-05 14:01:00 -080045
46 virtual const char* what() const noexcept override
47 {
Patrick Venture0ba794d2019-03-15 21:07:09 -070048 return _message.c_str();
Patrick Venture123b5c02019-03-05 14:01:00 -080049 }
50
Patrick Ventureef5e0922019-05-29 09:33:49 -070051 int code() const noexcept
52 {
53 return _ccode;
54 }
55
Patrick Venture123b5c02019-03-05 14:01:00 -080056 private:
Patrick Venture0ba794d2019-03-15 21:07:09 -070057 std::string _message;
Patrick Ventureef5e0922019-05-29 09:33:49 -070058 int _ccode = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080059};
60
Patrick Venture1470bec2019-03-06 07:33:12 -080061} // namespace ipmiblob