ExceptionHelper.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // System.Data.Common.ExceptionHelper
  3. //
  4. // Author:
  5. // Boris Kirzner ([email protected])
  6. //
  7. using System;
  8. using java.util;
  9. namespace System.Data.Common
  10. {
  11. internal sealed class ExceptionHelper
  12. {
  13. sealed class ResourceManager
  14. {
  15. private static readonly ResourceBundle _resourceBundle = ResourceBundle.getBundle("SystemData");
  16. internal ResourceManager()
  17. {
  18. }
  19. internal string GetString(string key)
  20. {
  21. return _resourceBundle.getString(key);
  22. }
  23. }
  24. static ResourceManager _resourceManager = new ResourceManager();
  25. internal static ArgumentException InvalidSizeValue(int value)
  26. {
  27. string[] args = new string[] {value.ToString()};
  28. return new ArgumentException(GetExceptionMessage("ADP_InvalidSizeValue",args));
  29. }
  30. internal static ArgumentOutOfRangeException InvalidDataRowVersion(DataRowVersion value)
  31. {
  32. return InvalidEnumerationValue(typeof(DataRowVersion), (int) value);
  33. }
  34. internal static ArgumentOutOfRangeException InvalidEnumerationValue(Type type, int value)
  35. {
  36. object[] args = new object[] { type.Name, value.ToString() } ;
  37. return new ArgumentOutOfRangeException(GetExceptionMessage("ADP_InvalidEnumerationValue",args));
  38. }
  39. internal static ArgumentException InvalidOffsetValue(int value)
  40. {
  41. string[] args = new string[] {value.ToString()};
  42. return new ArgumentException(GetExceptionMessage("ADP_InvalidOffsetValue",args));
  43. }
  44. internal static ArgumentOutOfRangeException InvalidParameterDirection(ParameterDirection value)
  45. {
  46. return InvalidEnumerationValue(typeof(ParameterDirection), (int) value);
  47. }
  48. internal static InvalidOperationException NoStoredProcedureExists(string procedureName) {
  49. object[] args = new object[1] { procedureName } ;
  50. return new InvalidOperationException(GetExceptionMessage("ADP_NoStoredProcedureExists", args));
  51. }
  52. internal static ArgumentNullException ArgumentNull(string parameter)
  53. {
  54. return new ArgumentNullException(parameter);
  55. }
  56. internal static InvalidOperationException TransactionRequired()
  57. {
  58. return new InvalidOperationException(GetExceptionMessage("ADP_TransactionRequired_Execute"));
  59. }
  60. internal static ArgumentOutOfRangeException InvalidOleDbType(int value)
  61. {
  62. string[] args = new string[] {value.ToString()};
  63. return new ArgumentOutOfRangeException(GetExceptionMessage("OleDb_InvalidOleDbType",args));
  64. }
  65. internal static ArgumentException InvalidDbType(int value)
  66. {
  67. string[] args = new string[] {value.ToString()};
  68. return new ArgumentException(GetExceptionMessage("ADP_UnknownDataType",args));
  69. }
  70. internal static InvalidOperationException DeriveParametersNotSupported(Type type,CommandType commandType)
  71. {
  72. string[] args = new string[] {type.ToString(),commandType.ToString()};
  73. return new InvalidOperationException(GetExceptionMessage("ADP_DeriveParametersNotSupported",args));
  74. }
  75. internal static InvalidOperationException ReaderClosed(string mehodName)
  76. {
  77. string[] args = new string[] {mehodName};
  78. return new InvalidOperationException(GetExceptionMessage("ADP_DataReaderClosed",args));
  79. }
  80. internal static ArgumentOutOfRangeException InvalidSqlDbType(int value)
  81. {
  82. string[] args = new string[] {value.ToString()};
  83. return new ArgumentOutOfRangeException(GetExceptionMessage("SQL_InvalidSqlDbType",args));
  84. }
  85. internal static ArgumentException UnknownDataType(string type1, string type2)
  86. {
  87. string[] args = new string[] {type1, type2};
  88. return new ArgumentException(GetExceptionMessage("ADP_UnknownDataType",args));
  89. }
  90. internal static InvalidOperationException TransactionNotInitialized()
  91. {
  92. return new InvalidOperationException(GetExceptionMessage("ADP_TransactionRequired_Execute"));
  93. }
  94. internal static InvalidOperationException ParametersNotInitialized(int parameterPosition,string parameterName,string parameterType)
  95. {
  96. object[] args = new object[] {parameterPosition,parameterName,parameterType};
  97. return new InvalidOperationException(GetExceptionMessage("OleDb_UninitializedParameters",args));
  98. }
  99. internal static InvalidOperationException WrongParameterSize(string provider)
  100. {
  101. string[] args = new string[] {provider};
  102. return new InvalidOperationException(GetExceptionMessage("ADP_PrepareParameterSize",args));
  103. }
  104. internal static InvalidOperationException ConnectionNotOpened(string operationName, string connectionState)
  105. {
  106. object[] args = new object[] {operationName,connectionState};
  107. return new InvalidOperationException(GetExceptionMessage("ADP_OpenConnectionRequired_PropertySet",args));
  108. }
  109. internal static InvalidOperationException ConnectionNotInitialized(string methodName)
  110. {
  111. object[] args = new object[] {methodName};
  112. return new InvalidOperationException(GetExceptionMessage("ADP_ConnectionRequired_ExecuteReader",args));
  113. }
  114. internal static InvalidOperationException OpenConnectionRequired(string methodName, object connectionState)
  115. {
  116. object[] args = new object[] {methodName, connectionState};
  117. return new InvalidOperationException(GetExceptionMessage("ADP_OpenConnectionRequired_Fill",args));
  118. }
  119. internal static InvalidOperationException OpenedReaderExists()
  120. {
  121. return new InvalidOperationException(GetExceptionMessage("ADP_OpenReaderExists"));
  122. }
  123. internal static InvalidOperationException ConnectionAlreadyOpen(object connectionState)
  124. {
  125. object[] args = new object[] {connectionState};
  126. return new InvalidOperationException(GetExceptionMessage("ADP_ConnectionAlreadyOpen",args));
  127. }
  128. internal static InvalidOperationException ConnectionStringNotInitialized()
  129. {
  130. return new InvalidOperationException(GetExceptionMessage("ADP_NoConnectionString"));
  131. }
  132. internal static InvalidOperationException ConnectionIsBusy(object commandType,object connectionState)
  133. {
  134. object[] args = new object[] {commandType.ToString(), connectionState.ToString()};
  135. return new InvalidOperationException(GetExceptionMessage("ADP_CommandIsActive",args));
  136. }
  137. internal static InvalidOperationException NotAllowedWhileConnectionOpen(string propertyName, object connectionState)
  138. {
  139. object[] args = new object[] {propertyName,connectionState};
  140. return new InvalidOperationException(GetExceptionMessage("ADP_OpenConnectionPropertySet",args));
  141. }
  142. internal static ArgumentException OleDbNoProviderSpecified()
  143. {
  144. return new ArgumentException(GetExceptionMessage("OleDb_NoProviderSpecified"));
  145. }
  146. internal static ArgumentException InvalidValueForKey(string key)
  147. {
  148. string[] args = new string[] { key };
  149. return new ArgumentException(String.Format("Invalid value for key {0}",args));
  150. }
  151. internal static InvalidOperationException ParameterSizeNotInitialized(int parameterIndex, string parameterName,string parameterType,int parameterSize)
  152. {
  153. object[] args = new object[] { parameterIndex.ToString(),parameterName,parameterType,parameterSize.ToString()};
  154. return new InvalidOperationException(GetExceptionMessage("ADP_UninitializedParameterSize",args));
  155. }
  156. internal static ArgumentException InvalidUpdateStatus(UpdateStatus status)
  157. {
  158. object[] args = new object[] { status };
  159. return new ArgumentException(GetExceptionMessage("ADP_InvalidUpdateStatus",args));
  160. }
  161. internal static InvalidOperationException UpdateRequiresCommand(string command)
  162. {
  163. return new InvalidOperationException(GetExceptionMessage("ADP_UpdateRequiresCommand" + command));
  164. }
  165. internal static DataException RowUpdatedError()
  166. {
  167. return new DataException(GetExceptionMessage("ADP_RowUpdatedErrors"));
  168. }
  169. internal static string GetExceptionMessage(string key,object[] args)
  170. {
  171. string exceptionMessage = _resourceManager.GetString(key);
  172. if ((args == null) || (args.Length == 0)) {
  173. return exceptionMessage;
  174. }
  175. else {
  176. return String.Format(exceptionMessage,args);
  177. }
  178. }
  179. internal static string GetExceptionMessage(string key)
  180. {
  181. return GetExceptionMessage(key,null);
  182. }
  183. }
  184. }