3 using System.Collections;
5 namespace TechBot.Library
7 [Command("error", Help = "!error <value>")]
8 public class ErrorCommand : Command
10 private NtStatusCommand ntStatus;
11 private WinErrorCommand winerror;
12 private HResultCommand hresult;
16 this.ntStatus = new NtStatusCommand();
17 this.winerror = new WinErrorCommand();
18 this.hresult = new HResultCommand();
21 private static int GetSeverity(long error)
23 return (int)((error >> 30) & 0x3);
26 private static bool IsCustomer(long error)
28 return (error & 0x20000000) != 0;
31 private static bool IsReserved(long error)
33 return (error & 0x10000000) != 0;
36 private static int GetFacility(long error)
38 return (int)((error >> 16) & 0xFFF);
41 private static short GetCode(long error)
43 return (short)((error >> 0) & 0xFFFF);
46 private static string FormatSeverity(long error)
48 int severity = GetSeverity(error);
51 case 0: return "SUCCESS";
52 case 1: return "INFORMATIONAL";
53 case 2: return "WARNING";
54 case 3: return "ERROR";
59 private static string FormatFacility(long error)
61 int facility = GetFacility(error);
62 return facility.ToString();
65 private static string FormatCode(long error)
67 int code = GetCode(error);
68 return code.ToString();
71 public override void Handle(MessageContext context)
73 if (Text.Equals(String.Empty))
75 TechBot.ServiceOutput.WriteLine(context,
76 "Please provide an Error Code.");
80 string errorText = originalErrorText;
83 NumberParser np = new NumberParser();
84 long error = np.Parse(errorText);
87 TechBot.ServiceOutput.WriteLine(context,
88 String.Format("{0} is not a valid Error Code.",
93 ArrayList descriptions = new ArrayList();
95 // Error is out of bounds
96 if ((ulong)error > uint.MaxValue)
100 // Error is outside of the range [0, 65535]: it cannot be a plain Win32 error code
101 else if ((ulong)error > ushort.MaxValue)
103 // Customer bit is set: custom error code
104 if (IsCustomer(error))
106 string description = String.Format("[custom, severity {0}, facility {1}, code {2}]",
107 FormatSeverity(error),
108 FormatFacility(error),
110 descriptions.Add(description);
112 // Reserved bit is set: HRESULT_FROM_NT(ntstatus)
113 else if (IsReserved(error))
115 int status = (int)(error & 0xCFFFFFFF);
116 string description = ntStatus.GetNtstatusDescription(status);
118 if (description == null)
119 description = status.ToString("X");
121 description = String.Format("HRESULT_FROM_NT({0})", description);
122 descriptions.Add(description);
124 // Win32 facility: HRESULT_FROM_WIN32(winerror)
125 else if (GetFacility(error) == 7)
127 // Must be an error code
128 if (GetSeverity(error) == 2)
130 short err = GetCode(error);
131 string description = winerror.GetWinerrorDescription(err);
133 if (description == null)
134 description = err.ToString("D");
136 description = String.Format("HRESULT_FROM_WIN32({0})", description);
137 descriptions.Add(description);
142 string winerrorDescription = winerror.GetWinerrorDescription(error);
143 string ntstatusDescription = ntStatus.GetNtstatusDescription(error);
144 string hresultDescription = hresult.GetHresultDescription(error);
146 if (winerrorDescription != null)
147 descriptions.Add(winerrorDescription);
148 if (ntstatusDescription != null)
149 descriptions.Add(ntstatusDescription);
150 if (hresultDescription != null)
151 descriptions.Add(hresultDescription);
153 if (descriptions.Count == 0)
155 // Last chance heuristics: attempt to parse a 8-digit decimal as hexadecimal
156 if (errorText.Length == 8)
158 errorText = "0x" + errorText;
162 TechBot.ServiceOutput.WriteLine(context,
163 String.Format("I don't know about Error Code {0}.",
166 else if (descriptions.Count == 1)
168 string description = (string)descriptions[0];
169 TechBot.ServiceOutput.WriteLine(context,
170 String.Format("{0} is {1}.",
176 TechBot.ServiceOutput.WriteLine(context,
177 String.Format("{0} could be:",
180 foreach(string description in descriptions)
181 TechBot.ServiceOutput.WriteLine(context, String.Format("\t{0}", description));