-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
LiteException.cs
323 lines (264 loc) · 13.3 KB
/
LiteException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using LiteDB.Engine;
using System;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static LiteDB.Constants;
namespace LiteDB
{
/// <summary>
/// The main exception for LiteDB
/// </summary>
public class LiteException : Exception
{
#region Errors code
public const int FILE_NOT_FOUND = 101;
public const int DATABASE_SHUTDOWN = 102;
public const int INVALID_DATABASE = 103;
public const int FILE_SIZE_EXCEEDED = 105;
public const int COLLECTION_LIMIT_EXCEEDED = 106;
public const int INDEX_DROP_ID = 108;
public const int INDEX_DUPLICATE_KEY = 110;
public const int INVALID_INDEX_KEY = 111;
public const int INDEX_NOT_FOUND = 112;
public const int INVALID_DBREF = 113;
public const int LOCK_TIMEOUT = 120;
public const int INVALID_COMMAND = 121;
public const int ALREADY_EXISTS_COLLECTION_NAME = 122;
public const int ALREADY_OPEN_DATAFILE = 124;
public const int INVALID_TRANSACTION_STATE = 126;
public const int INDEX_NAME_LIMIT_EXCEEDED = 128;
public const int INVALID_INDEX_NAME = 129;
public const int INVALID_COLLECTION_NAME = 130;
public const int TEMP_ENGINE_ALREADY_DEFINED = 131;
public const int INVALID_EXPRESSION_TYPE = 132;
public const int COLLECTION_NOT_FOUND = 133;
public const int COLLECTION_ALREADY_EXIST = 134;
public const int INDEX_ALREADY_EXIST = 135;
public const int INVALID_UPDATE_FIELD = 136;
public const int INVALID_FORMAT = 200;
public const int DOCUMENT_MAX_DEPTH = 201;
public const int INVALID_CTOR = 202;
public const int UNEXPECTED_TOKEN = 203;
public const int INVALID_DATA_TYPE = 204;
public const int PROPERTY_NOT_MAPPED = 206;
public const int INVALID_TYPED_NAME = 207;
public const int PROPERTY_READ_WRITE = 209;
public const int INITIALSIZE_CRYPTO_NOT_SUPPORTED = 210;
public const int INVALID_INITIALSIZE = 211;
public const int INVALID_NULL_CHAR_STRING = 212;
public const int INVALID_FREE_SPACE_PAGE = 213;
public const int DATA_TYPE_NOT_ASSIGNABLE = 214;
public const int AVOID_USE_OF_PROCESS = 215;
#endregion
#region Ctor
public int ErrorCode { get; private set; }
public long Position { get; private set; }
public LiteException(int code, string message)
: base(message)
{
this.ErrorCode = code;
}
internal LiteException(int code, string message, params object[] args)
: base(string.Format(message, args))
{
this.ErrorCode = code;
}
internal LiteException (int code, Exception inner, string message, params object[] args)
: base (string.Format (message, args), inner)
{
this.ErrorCode = code;
}
#endregion
#region Method Errors
internal static LiteException FileNotFound(object fileId)
{
return new LiteException(FILE_NOT_FOUND, "File '{0}' not found.", fileId);
}
internal static LiteException DatabaseShutdown()
{
return new LiteException(DATABASE_SHUTDOWN, "Database is in shutdown process.");
}
internal static LiteException InvalidDatabase()
{
return new LiteException(INVALID_DATABASE, "File is not a valid LiteDB database format or contains a invalid password.");
}
internal static LiteException FileSizeExceeded(long limit)
{
return new LiteException(FILE_SIZE_EXCEEDED, "Database size exceeds limit of {0}.", FileHelper.FormatFileSize(limit));
}
internal static LiteException CollectionLimitExceeded(int limit)
{
return new LiteException(COLLECTION_LIMIT_EXCEEDED, "This database exceeded the maximum limit of collection names size: {0} bytes", limit);
}
internal static LiteException IndexNameLimitExceeded(int limit)
{
return new LiteException(INDEX_NAME_LIMIT_EXCEEDED, "This collection exceeded the maximum limit of indexes names/expression size: {0} bytes", limit);
}
internal static LiteException InvalidIndexName(string name, string collection, string reason)
{
return new LiteException(INVALID_INDEX_NAME, "Invalid index name '{0}' on collection '{1}': {2}", name, collection, reason);
}
internal static LiteException InvalidCollectionName(string name, string reason)
{
return new LiteException(INVALID_COLLECTION_NAME, "Invalid collection name '{0}': {1}", name, reason);
}
internal static LiteException IndexDropId()
{
return new LiteException(INDEX_DROP_ID, "Primary key index '_id' can't be dropped.");
}
internal static LiteException TempEngineAlreadyDefined()
{
return new LiteException(TEMP_ENGINE_ALREADY_DEFINED, "Temporary engine already defined or auto created.");
}
internal static LiteException CollectionNotFound(string key)
{
return new LiteException(COLLECTION_NOT_FOUND, "Collection not found: '{0}'", key);
}
internal static LiteException InvalidExpressionType(BsonExpression expr, BsonExpressionType type)
{
return new LiteException(INVALID_EXPRESSION_TYPE, "Expression '{0}' must be a {1} type.", expr.Source, type);
}
internal static LiteException InvalidExpressionTypePredicate(BsonExpression expr)
{
return new LiteException(INVALID_EXPRESSION_TYPE, "Expression '{0}' are not supported as predicate expression.", expr.Source);
}
internal static LiteException CollectionAlreadyExist(string key)
{
return new LiteException(COLLECTION_ALREADY_EXIST, "Collection already exist: '{0}'", key);
}
internal static LiteException IndexAlreadyExist(string name)
{
return new LiteException(INDEX_ALREADY_EXIST, "Index name '{0}' already exist with a differnt expression. Try drop index first.", name);
}
internal static LiteException InvalidUpdateField(string field)
{
return new LiteException(INVALID_UPDATE_FIELD, "'{0}' can't be modified in UPDATE command.", field);
}
internal static LiteException IndexDuplicateKey(string field, BsonValue key)
{
return new LiteException(INDEX_DUPLICATE_KEY, "Cannot insert duplicate key in unique index '{0}'. The duplicate value is '{1}'.", field, key);
}
internal static LiteException InvalidIndexKey(string text)
{
return new LiteException(INVALID_INDEX_KEY, text);
}
internal static LiteException IndexNotFound(string name)
{
return new LiteException(INDEX_NOT_FOUND, "Index not found '{0}'.", name);
}
internal static LiteException LockTimeout(string mode, TimeSpan ts)
{
return new LiteException(LOCK_TIMEOUT, "Database lock timeout when entering in {0} mode after {1}", mode, ts.ToString());
}
internal static LiteException LockTimeout(string mode, string collection, TimeSpan ts)
{
return new LiteException(LOCK_TIMEOUT, "Collection '{0}' lock timeout when entering in {1} mode after {2}", collection, mode, ts.ToString());
}
internal static LiteException InvalidCommand(string command)
{
return new LiteException(INVALID_COMMAND, "Command '{0}' is not a valid shell command.", command);
}
internal static LiteException AlreadyExistsCollectionName(string newName)
{
return new LiteException(ALREADY_EXISTS_COLLECTION_NAME, "New collection name '{0}' already exists.", newName);
}
internal static LiteException AlreadyOpenDatafile(string filename)
{
return new LiteException(ALREADY_OPEN_DATAFILE, "Your datafile '{0}' is open in another process.", filename);
}
internal static LiteException InvalidDbRef(string path)
{
return new LiteException(INVALID_DBREF, "Invalid value for DbRef in path '{0}'. Value must be document like {{ $ref: \"?\", $id: ? }}", path);
}
internal static LiteException AlreadyExistsTransaction()
{
return new LiteException(INVALID_TRANSACTION_STATE, "The current thread already contains an open transaction. Use the Commit/Rollback method to release the previous transaction.");
}
internal static LiteException CollectionLockerNotFound(string collection)
{
return new LiteException(INVALID_TRANSACTION_STATE, "Collection locker '{0}' was not found inside dictionary.", collection);
}
internal static LiteException InvalidFormat(string field)
{
return new LiteException(INVALID_FORMAT, "Invalid format: {0}", field);
}
internal static LiteException DocumentMaxDepth(int depth, Type type)
{
return new LiteException(DOCUMENT_MAX_DEPTH, "Document has more than {0} nested documents in '{1}'. Check for circular references (use DbRef).", depth, type == null ? "-" : type.Name);
}
internal static LiteException InvalidCtor(Type type, Exception inner)
{
return new LiteException(INVALID_CTOR, inner, "Failed to create instance for type '{0}' from assembly '{1}'. Checks if the class has a public constructor with no parameters.", type.FullName, type.AssemblyQualifiedName);
}
internal static LiteException UnexpectedToken(Token token, string expected = null)
{
var position = (token?.Position - (token?.Value?.Length ?? 0)) ?? 0;
var str = token?.Type == TokenType.EOF ? "[EOF]" : token?.Value ?? "";
var exp = expected == null ? "" : $" Expected `{expected}`.";
return new LiteException(UNEXPECTED_TOKEN, $"Unexpected token `{str}` in position {position}.{exp}")
{
Position = position
};
}
internal static LiteException UnexpectedToken(string message, Token token)
{
var position = (token?.Position - (token?.Value?.Length ?? 0)) ?? 0;
return new LiteException(UNEXPECTED_TOKEN, message)
{
Position = position
};
}
internal static LiteException InvalidDataType(string field, BsonValue value)
{
return new LiteException(INVALID_DATA_TYPE, "Invalid BSON data type '{0}' on field '{1}'.", value.Type, field);
}
internal static LiteException PropertyReadWrite(PropertyInfo prop)
{
return new LiteException(PROPERTY_READ_WRITE, "'{0}' property must have public getter and setter.", prop.Name);
}
internal static LiteException PropertyNotMapped(string name)
{
return new LiteException(PROPERTY_NOT_MAPPED, "Property '{0}' was not mapped into BsonDocument.", name);
}
internal static LiteException InvalidTypedName(string type)
{
return new LiteException(INVALID_TYPED_NAME, "Type '{0}' not found in current domain (_type format is 'Type.FullName, AssemblyName').", type);
}
internal static LiteException InitialSizeCryptoNotSupported()
{
return new LiteException(INITIALSIZE_CRYPTO_NOT_SUPPORTED, "Initial Size option is not supported for encrypted datafiles.");
}
internal static LiteException InvalidInitialSize()
{
return new LiteException(INVALID_INITIALSIZE, "Initial Size must be a multiple of page size ({0} bytes).", PAGE_SIZE);
}
internal static LiteException InvalidNullCharInString()
{
return new LiteException(INVALID_NULL_CHAR_STRING, "Invalid null character (\\0) was found in the string");
}
internal static LiteException InvalidPageType(PageType pageType, BasePage page)
{
var sb = new StringBuilder($"Invalid {pageType} on {page.PageID}. ");
sb.Append($"Full zero: {page.Buffer.All(0)}. ");
sb.Append($"Page Type: {page.PageType}. ");
sb.Append($"Prev/Next: {page.PrevPageID}/{page.NextPageID}. ");
sb.Append($"UniqueID: {page.Buffer.UniqueID}. ");
sb.Append($"ShareCounter: {page.Buffer.ShareCounter}. ");
return new LiteException(0, sb.ToString());
}
internal static LiteException InvalidFreeSpacePage(uint pageID, int freeBytes, int length)
{
return new LiteException(INVALID_FREE_SPACE_PAGE, $"An operation that would corrupt page {pageID} was prevented. The operation required {length} free bytes, but the page had only {freeBytes} available.");
}
internal static LiteException DataTypeNotAssignable(string type1, string type2)
{
return new LiteException(DATA_TYPE_NOT_ASSIGNABLE, $"Data type {type1} is not assignable from data type {type2}");
}
internal static LiteException AvoidUseOfProcess()
{
return new LiteException(AVOID_USE_OF_PROCESS, $"LiteDB do not accept System.Diagnostics.Process class in deserialize mapper");
}
#endregion
}
}