ExceptionHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // System.Data.Common.ExceptionHelper
  3. //
  4. // Author:
  5. // Boris Kirzner ([email protected])
  6. //
  7. using System;
  8. using System.Globalization;
  9. namespace System.Data.Common
  10. {
  11. internal sealed class ExceptionHelper
  12. {
  13. internal static ArgumentException InvalidSizeValue (int value)
  14. {
  15. string [] args = new string [] {value.ToString ()};
  16. return new ArgumentException (GetExceptionMessage ("Invalid parameter Size value '{0}'. The value must be greater than or equal to 0.",args));
  17. }
  18. internal static void CheckEnumValue (Type enumType, object value)
  19. {
  20. if (!Enum.IsDefined (enumType, value))
  21. throw InvalidEnumValueException (enumType.Name, value);
  22. }
  23. internal static ArgumentException InvalidEnumValueException (String enumeration, object value)
  24. {
  25. #if NET_2_0
  26. return new ArgumentOutOfRangeException (enumeration,
  27. string.Format (CultureInfo.InvariantCulture,
  28. "The {0} enumeration value, {1}, is " +
  29. "invalid", enumeration, value));
  30. #else
  31. return new ArgumentException (String.Format ("The {0} enumeration value, {1}, is invalid", enumeration, value));
  32. #endif
  33. }
  34. internal static ArgumentOutOfRangeException InvalidDataRowVersion (DataRowVersion value)
  35. {
  36. object [] args = new object [] { "DataRowVersion", value.ToString () } ;
  37. return new ArgumentOutOfRangeException (GetExceptionMessage ("{0}: Invalid DataRow Version enumeration value: {1}",args));
  38. }
  39. internal static ArgumentOutOfRangeException InvalidParameterDirection (ParameterDirection value)
  40. {
  41. object [] args = new object [] { "ParameterDirection", value.ToString () } ;
  42. return new ArgumentOutOfRangeException (GetExceptionMessage ("Invalid direction '{0}' for '{1}' parameter.",args));
  43. }
  44. internal static InvalidOperationException NoStoredProcedureExists (string procedureName) {
  45. object [] args = new object [1] { procedureName } ;
  46. return new InvalidOperationException (GetExceptionMessage ("The stored procedure '{0}' doesn't exist.", args));
  47. }
  48. internal static ArgumentNullException ArgumentNull (string parameter)
  49. {
  50. return new ArgumentNullException (parameter);
  51. }
  52. internal static InvalidOperationException TransactionRequired ()
  53. {
  54. return new InvalidOperationException (GetExceptionMessage ("Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."));
  55. }
  56. internal static ArgumentOutOfRangeException InvalidOleDbType (int value)
  57. {
  58. string [] args = new string [] { value.ToString() };
  59. return new ArgumentOutOfRangeException (GetExceptionMessage ("Invalid OleDbType enumeration value: {0}",args));
  60. }
  61. internal static ArgumentException InvalidDbType(int value)
  62. {
  63. string [] args = new string [] { value.ToString () };
  64. return new ArgumentException (GetExceptionMessage ("No mapping exists from DbType {0} to a known {1}.",args));
  65. }
  66. internal static InvalidOperationException DeriveParametersNotSupported(Type type,CommandType commandType)
  67. {
  68. string [] args = new string [] { type.ToString(),commandType.ToString() };
  69. return new InvalidOperationException (GetExceptionMessage ("{0} DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{1}.",args));
  70. }
  71. internal static InvalidOperationException ReaderClosed (string mehodName)
  72. {
  73. string [] args = new string [] { mehodName };
  74. return new InvalidOperationException (GetExceptionMessage ("Invalid attempt to {0} when reader is closed.",args));
  75. }
  76. internal static ArgumentOutOfRangeException InvalidSqlDbType (int value)
  77. {
  78. string [] args = new string [] { value.ToString () };
  79. return new ArgumentOutOfRangeException (GetExceptionMessage ("{0}: Invalid SqlDbType enumeration value: {1}.",args));
  80. }
  81. internal static ArgumentException UnknownDataType (string type1, string type2)
  82. {
  83. string [] args = new string [] { type1, type2 };
  84. return new ArgumentException (GetExceptionMessage ("No mapping exists from DbType {0} to a known {1}.",args));
  85. }
  86. internal static InvalidOperationException TransactionNotInitialized ()
  87. {
  88. return new InvalidOperationException (GetExceptionMessage ("Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."));
  89. }
  90. internal static InvalidOperationException TransactionNotUsable (Type type)
  91. {
  92. return new InvalidOperationException (string.Format (
  93. CultureInfo.InvariantCulture,
  94. "This {0} has completed; it is no longer usable.",
  95. type.Name));
  96. }
  97. internal static InvalidOperationException ParametersNotInitialized (int parameterPosition,string parameterName,string parameterType)
  98. {
  99. object [] args = new object [] { parameterPosition, parameterName, parameterType };
  100. return new InvalidOperationException (GetExceptionMessage ("Parameter {0}: '{1}', the property DbType is uninitialized: OleDbType.{2}.",args));
  101. }
  102. internal static InvalidOperationException WrongParameterSize(string provider)
  103. {
  104. string [] args = new string [] { provider };
  105. return new InvalidOperationException (GetExceptionMessage ("{0}.Prepare method requires all variable length parameters to have an explicitly set non-zero Size.",args));
  106. }
  107. internal static InvalidOperationException ConnectionNotOpened (string operationName, string connectionState)
  108. {
  109. object [] args = new object [] { operationName, connectionState };
  110. return new InvalidOperationException (GetExceptionMessage ("{0} requires an open and available Connection. The connection's current state is {1}.",args));
  111. }
  112. internal static InvalidOperationException ConnectionNotInitialized (string methodName)
  113. {
  114. object [] args = new object [] { methodName };
  115. return new InvalidOperationException (GetExceptionMessage ("{0}: Connection property has not been initialized.",args));
  116. }
  117. internal static InvalidOperationException OpenConnectionRequired (string methodName, object connectionState)
  118. {
  119. object [] args = new object [] { methodName, connectionState };
  120. return new InvalidOperationException (GetExceptionMessage ("{0} requires an open and available Connection. The connection's current state is {1}.",args));
  121. }
  122. internal static InvalidOperationException OpenedReaderExists ()
  123. {
  124. return new InvalidOperationException (GetExceptionMessage ("There is already an open DataReader associated with this Connection which must be closed first."));
  125. }
  126. internal static InvalidOperationException ConnectionAlreadyOpen (object connectionState)
  127. {
  128. object [] args = new object [] { connectionState };
  129. return new InvalidOperationException (GetExceptionMessage ("The connection is already Open (state={0}).",args));
  130. }
  131. internal static InvalidOperationException ConnectionClosed ()
  132. {
  133. return new InvalidOperationException ("Invalid operation. The Connection is closed.");
  134. }
  135. internal static InvalidOperationException ConnectionStringNotInitialized ()
  136. {
  137. return new InvalidOperationException (GetExceptionMessage ("The ConnectionString property has not been initialized."));
  138. }
  139. internal static InvalidOperationException ConnectionIsBusy (object commandType,object connectionState)
  140. {
  141. object [] args = new object [] { commandType.ToString (), connectionState.ToString () };
  142. return new InvalidOperationException (GetExceptionMessage ("The {0} is currently busy {1}.",args));
  143. }
  144. internal static InvalidOperationException NotAllowedWhileConnectionOpen (string propertyName, object connectionState)
  145. {
  146. object [] args = new object [] { propertyName, connectionState };
  147. return new InvalidOperationException (GetExceptionMessage ("Not allowed to change the '{0}' property while the connection (state={1}).",args));
  148. }
  149. internal static ArgumentException OleDbNoProviderSpecified ()
  150. {
  151. return new ArgumentException (GetExceptionMessage ("An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'."));
  152. }
  153. internal static ArgumentException InvalidValueForKey (string key)
  154. {
  155. string [] args = new string [] { key };
  156. return new ArgumentException (String.Format ("Invalid value for key {0}",args));
  157. }
  158. internal static InvalidOperationException ParameterSizeNotInitialized( int parameterIndex, string parameterName,string parameterType,int parameterSize)
  159. {
  160. object [] args = new object [] { parameterIndex.ToString (), parameterName, parameterType, parameterSize.ToString () };
  161. return new InvalidOperationException (GetExceptionMessage ("Parameter {0}: '{1}' of type: {2}, the property Size has an invalid size: {3}",args));
  162. }
  163. internal static ArgumentException InvalidUpdateStatus (UpdateStatus status)
  164. {
  165. object [] args = new object [] { status };
  166. return new ArgumentException (GetExceptionMessage ("Invalid UpdateStatus: {0}",args));
  167. }
  168. internal static InvalidOperationException UpdateRequiresCommand (string command)
  169. {
  170. object [] args = new object [] { command };
  171. return new InvalidOperationException (GetExceptionMessage ("Auto SQL generation during {0} requires a valid SelectCommand.",args));
  172. }
  173. internal static DataException RowUpdatedError ()
  174. {
  175. return new DataException (GetExceptionMessage ("RowUpdatedEvent: Errors occurred; no additional is information available."));
  176. }
  177. internal static ArgumentNullException CollectionNoNullsAllowed (object collection, object objectsType)
  178. {
  179. object [] args = new object [] {collection.GetType ().ToString (), objectsType.ToString ()};
  180. return new ArgumentNullException (GetExceptionMessage ("The {0} only accepts non-null {1} type objects."));
  181. }
  182. internal static ArgumentException CollectionAlreadyContains(object objectType,string propertyName, object propertyValue, object collection)
  183. {
  184. object [] args = new object [] {objectType.ToString (), propertyName, propertyValue, collection.GetType ().ToString ()};
  185. return new ArgumentException (GetExceptionMessage ("The {0} with {1} '{2}' is already contained by this {3}.",args));
  186. }
  187. internal static string GetExceptionMessage (string exceptionMessage,object [] args)
  188. {
  189. if ((args == null) || (args.Length == 0)) {
  190. return exceptionMessage;
  191. } else {
  192. return String.Format (exceptionMessage,args);
  193. }
  194. }
  195. internal static string GetExceptionMessage (string exceptionMessage)
  196. {
  197. return GetExceptionMessage (exceptionMessage, null);
  198. }
  199. }
  200. }