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