ExceptionHelper.cs 9.7 KB

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