ExceptionHelper.cs 10 KB

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