ExceptionHelper.cs 9.3 KB

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