PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/MSBuild/Microsoft/Build/Shared/ErrorUtilities.cs

#
C# | 278 lines | 240 code | 38 blank | 0 comment | 40 complexity | 36b45a26f77c371ba68045dd4ec8dc3d MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.Build.Shared
  2. {
  3. using System;
  4. using System.IO;
  5. internal static class ErrorUtilities
  6. {
  7. private static readonly bool throwExceptions = string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDDONOTTHROWINTERNAL"));
  8. internal static void ThrowArgument(string resourceName, params object[] args)
  9. {
  10. ThrowArgument(null, resourceName, args);
  11. }
  12. private static void ThrowArgument(Exception innerException, string resourceName, params object[] args)
  13. {
  14. if (throwExceptions)
  15. {
  16. throw new ArgumentException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString(resourceName, args), innerException);
  17. }
  18. }
  19. internal static void ThrowArgumentOutOfRange(string parameterName)
  20. {
  21. if (throwExceptions)
  22. {
  23. throw new ArgumentOutOfRangeException(parameterName);
  24. }
  25. }
  26. internal static void ThrowIfTypeDoesNotImplementToString(object param)
  27. {
  28. }
  29. internal static void ThrowInternalError(string message, params object[] args)
  30. {
  31. if (throwExceptions)
  32. {
  33. throw new Microsoft.Build.Shared.InternalErrorException(Microsoft.Build.Shared.ResourceUtilities.FormatString(message, args));
  34. }
  35. }
  36. internal static void ThrowInternalError(string message, Exception innerException, params object[] args)
  37. {
  38. if (throwExceptions)
  39. {
  40. throw new Microsoft.Build.Shared.InternalErrorException(Microsoft.Build.Shared.ResourceUtilities.FormatString(message, args), innerException);
  41. }
  42. }
  43. internal static void ThrowInternalErrorUnreachable()
  44. {
  45. if (throwExceptions)
  46. {
  47. throw new Microsoft.Build.Shared.InternalErrorException("Unreachable?");
  48. }
  49. }
  50. internal static void ThrowInvalidOperation(string resourceName, params object[] args)
  51. {
  52. if (throwExceptions)
  53. {
  54. throw new InvalidOperationException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString(resourceName, args));
  55. }
  56. }
  57. internal static void VerifyThrow(bool condition, string unformattedMessage)
  58. {
  59. if (!condition)
  60. {
  61. ThrowInternalError(unformattedMessage, null, null);
  62. }
  63. }
  64. internal static void VerifyThrow(bool condition, string unformattedMessage, object arg0)
  65. {
  66. if (!condition)
  67. {
  68. ThrowInternalError(unformattedMessage, new object[] { arg0 });
  69. }
  70. }
  71. internal static void VerifyThrow(bool condition, string unformattedMessage, object arg0, object arg1)
  72. {
  73. if (!condition)
  74. {
  75. ThrowInternalError(unformattedMessage, new object[] { arg0, arg1 });
  76. }
  77. }
  78. internal static void VerifyThrow(bool condition, string unformattedMessage, object arg0, object arg1, object arg2)
  79. {
  80. if (!condition)
  81. {
  82. ThrowInternalError(unformattedMessage, new object[] { arg0, arg1, arg2 });
  83. }
  84. }
  85. internal static void VerifyThrow(bool condition, string unformattedMessage, object arg0, object arg1, object arg2, object arg3)
  86. {
  87. if (!condition)
  88. {
  89. ThrowInternalError(unformattedMessage, new object[] { arg0, arg1, arg2, arg3 });
  90. }
  91. }
  92. internal static void VerifyThrowArgument(bool condition, string resourceName)
  93. {
  94. VerifyThrowArgument(condition, null, resourceName);
  95. }
  96. internal static void VerifyThrowArgument(bool condition, Exception innerException, string resourceName)
  97. {
  98. if (!condition)
  99. {
  100. ThrowArgument(innerException, resourceName, null);
  101. }
  102. }
  103. internal static void VerifyThrowArgument(bool condition, string resourceName, object arg0)
  104. {
  105. VerifyThrowArgument(condition, null, resourceName, arg0);
  106. }
  107. internal static void VerifyThrowArgument(bool condition, Exception innerException, string resourceName, object arg0)
  108. {
  109. if (!condition)
  110. {
  111. ThrowArgument(innerException, resourceName, new object[] { arg0 });
  112. }
  113. }
  114. internal static void VerifyThrowArgument(bool condition, string resourceName, object arg0, object arg1)
  115. {
  116. VerifyThrowArgument(condition, null, resourceName, arg0, arg1);
  117. }
  118. internal static void VerifyThrowArgument(bool condition, Exception innerException, string resourceName, object arg0, object arg1)
  119. {
  120. if (!condition)
  121. {
  122. ThrowArgument(innerException, resourceName, new object[] { arg0, arg1 });
  123. }
  124. }
  125. internal static void VerifyThrowArgument(bool condition, string resourceName, object arg0, object arg1, object arg2)
  126. {
  127. VerifyThrowArgument(condition, null, resourceName, arg0, arg1, arg2);
  128. }
  129. internal static void VerifyThrowArgument(bool condition, Exception innerException, string resourceName, object arg0, object arg1, object arg2)
  130. {
  131. if (!condition)
  132. {
  133. ThrowArgument(innerException, resourceName, new object[] { arg0, arg1, arg2 });
  134. }
  135. }
  136. internal static void VerifyThrowArgument(bool condition, string resourceName, object arg0, object arg1, object arg2, object arg3)
  137. {
  138. VerifyThrowArgument(condition, null, resourceName, arg0, arg1, arg2, arg3);
  139. }
  140. internal static void VerifyThrowArgument(bool condition, Exception innerException, string resourceName, object arg0, object arg1, object arg2, object arg3)
  141. {
  142. if (!condition)
  143. {
  144. ThrowArgument(innerException, resourceName, new object[] { arg0, arg1, arg2, arg3 });
  145. }
  146. }
  147. internal static void VerifyThrowArgumentArraysSameLength(Array parameter1, Array parameter2, string parameter1Name, string parameter2Name)
  148. {
  149. VerifyThrowArgumentNull(parameter1, parameter1Name);
  150. VerifyThrowArgumentNull(parameter2, parameter2Name);
  151. if ((parameter1.Length != parameter2.Length) && throwExceptions)
  152. {
  153. throw new ArgumentException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("Shared.ParametersMustHaveTheSameLength", new object[] { parameter1Name, parameter2Name }));
  154. }
  155. }
  156. internal static void VerifyThrowArgumentLength(string parameter, string parameterName)
  157. {
  158. VerifyThrowArgumentNull(parameter, parameterName);
  159. if ((parameter.Length == 0) && throwExceptions)
  160. {
  161. throw new ArgumentException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("Shared.ParameterCannotHaveZeroLength", new object[] { parameterName }));
  162. }
  163. }
  164. internal static void VerifyThrowArgumentLengthIfNotNull(string parameter, string parameterName)
  165. {
  166. if (((parameter != null) && (parameter.Length == 0)) && throwExceptions)
  167. {
  168. throw new ArgumentException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("Shared.ParameterCannotHaveZeroLength", new object[] { parameterName }));
  169. }
  170. }
  171. internal static void VerifyThrowArgumentNull(object parameter, string parameterName)
  172. {
  173. VerifyThrowArgumentNull(parameter, parameterName, "Shared.ParameterCannotBeNull");
  174. }
  175. internal static void VerifyThrowArgumentNull(object parameter, string parameterName, string resourceName)
  176. {
  177. if ((parameter == null) && throwExceptions)
  178. {
  179. throw new Exception(); // new ArgumentNullException(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString(resourceName, new object[] { parameterName }), null);
  180. }
  181. }
  182. internal static void VerifyThrowArgumentOutOfRange(bool condition, string parameterName)
  183. {
  184. if (!condition)
  185. {
  186. ThrowArgumentOutOfRange(parameterName);
  187. }
  188. }
  189. internal static void VerifyThrowInternalLength(string parameterValue, string parameterName)
  190. {
  191. VerifyThrowInternalNull(parameterValue, parameterName);
  192. if (parameterValue.Length == 0)
  193. {
  194. ThrowInternalError("{0} unexpectedly empty", new object[] { parameterName });
  195. }
  196. }
  197. internal static void VerifyThrowInternalNull(object parameter, string parameterName)
  198. {
  199. if (parameter == null)
  200. {
  201. ThrowInternalError("{0} unexpectedly null", new object[] { parameterName });
  202. }
  203. }
  204. internal static void VerifyThrowInternalRooted(string value)
  205. {
  206. if (!Path.IsPathRooted(value))
  207. {
  208. ThrowInternalError("{0} unexpectedly not a rooted path", new object[] { value });
  209. }
  210. }
  211. internal static void VerifyThrowInvalidOperation(bool condition, string resourceName)
  212. {
  213. if (!condition)
  214. {
  215. ThrowInvalidOperation(resourceName, null);
  216. }
  217. }
  218. internal static void VerifyThrowInvalidOperation(bool condition, string resourceName, object arg0)
  219. {
  220. if (!condition)
  221. {
  222. ThrowInvalidOperation(resourceName, new object[] { arg0 });
  223. }
  224. }
  225. internal static void VerifyThrowInvalidOperation(bool condition, string resourceName, object arg0, object arg1)
  226. {
  227. if (!condition)
  228. {
  229. ThrowInvalidOperation(resourceName, new object[] { arg0, arg1 });
  230. }
  231. }
  232. internal static void VerifyThrowInvalidOperation(bool condition, string resourceName, object arg0, object arg1, object arg2)
  233. {
  234. if (!condition)
  235. {
  236. ThrowInvalidOperation(resourceName, new object[] { arg0, arg1, arg2 });
  237. }
  238. }
  239. }
  240. }