Skip to content

Commit 5ee9432

Browse files
author
Hugo Bonacci
committed
Replaced 'LameException' with meaningful exception names
1 parent 72e1da9 commit 5ee9432

File tree

10 files changed

+29
-38
lines changed

10 files changed

+29
-38
lines changed

Bson/BsonObject.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,10 @@ private static byte[] _BinarySerialize<T>(T value) where T : class {
478478
}
479479
}
480480
//notify if they cannot serialize this object
481-
catch (SerializationException ex) {
482-
throw new LameException("Not allowed to seralize");
483-
}
484-
//notify for other unexpected errors
485481
catch (Exception ex) {
486-
throw new LameException("Failed to serialize object");
482+
throw new MongoSerializationException(
483+
value == null ? "null" : value.GetType().Name,
484+
ex);
487485
}
488486

489487
}
@@ -498,9 +496,9 @@ private static object _BinaryDeserialize(byte[] bytes) {
498496
return formatter.Deserialize(input);
499497
}
500498
}
501-
//notify for other unexpected errors
499+
//notify if they cannot serialize this object
502500
catch (Exception ex) {
503-
throw new LameException("Failed to serialize object");
501+
throw new MongoDeserializationException(ex);
504502
}
505503

506504
}

Commands/MongoDatabaseCommands.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static MethodResult Authenticate(MongoDatabase database, string username,
4141
//get the values required
4242
GetNonceResult nonce = MongoDatabaseCommands.GetNonce(database);
4343
if (!nonce.HasNonce) {
44-
throw new LameException("Missing nonce from database!");
44+
throw new MongoServerException(string.Format("Unable to get a nonce value from {0}.", database.Connection.Host));
4545
}
4646

4747
//issue the actual command
@@ -375,8 +375,12 @@ public static CommandResponse RunCommand(MongoDatabase database, BsonObject para
375375

376376
//send the command and check for the result
377377
CommandResponse response = database.SendRequest(request) as CommandResponse;
378-
if (response == null) {
379-
throw new LameException("Invoking command failed to return a response!");
378+
if (response == null && expectResponse) {
379+
throw new MongoServerException(
380+
string.Format(
381+
"The request to {0} expected a response but nothing was returned!",
382+
database.Connection.Host
383+
));
380384
}
381385

382386
//return any documents that were found

DataTypes/MongoDataType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static void RegisterMongoDataType<T>() where T : MongoDataType {
127127

128128
//make sure this isn't already added
129129
if (MongoDataType.IsTypeRegistered<T>()) {
130-
throw new LameException("This type is already in here");
130+
throw new MongoTypeAlreadyRegisteredException(typeof(T));
131131
}
132132

133133
//add the instance

Exceptions/LameException.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

Mongo.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@
4747
</ItemGroup>
4848
<ItemGroup>
4949
<Compile Include="Bson\BsonAnonymousTypeParser.cs" />
50+
<Compile Include="Exceptions\ConnectionNotOpenedException.cs" />
51+
<Compile Include="Exceptions\InvalidMongoConnectionStringException.cs" />
52+
<Compile Include="Exceptions\MissingCollectionNameException.cs" />
53+
<Compile Include="Exceptions\MongoTypeAlreadyRegisteredException.cs" />
54+
<Compile Include="Exceptions\MongoDeserializationException.cs" />
55+
<Compile Include="Exceptions\MongoSerializationException.cs" />
5056
<Compile Include="Exceptions\MongoServerException.cs" />
57+
<Compile Include="Exceptions\NoCursorsFoundException.cs" />
5158
<Compile Include="Extensions\Queries\MongoQueryExtensions.cs" />
5259
<Compile Include="MongoAdminDatabase.cs" />
5360
<Compile Include="Bson\BsonCollection.cs" />
@@ -76,7 +83,6 @@
7683
<Compile Include="DataTypes\MongoObjectType.cs" />
7784
<Compile Include="DataTypes\MongoOidType.cs" />
7885
<Compile Include="DataTypes\MongoStringType.cs" />
79-
<Compile Include="Exceptions\LameException.cs" />
8086
<Compile Include="Extensions\EnumerationExtensions.cs" />
8187
<Compile Include="Helpers\Helpers.cs" />
8288
<Compile Include="MongoCollection.cs" />

MongoConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public ResponseBase SendRequest(RequestBase request) {
158158

159159
//perform normal checking
160160
if (!this.Connected) {
161-
throw new LameException("Connection isn't open yet!");
161+
throw new ConnectionNotOpenedException("Connection isn't open yet!");
162162
}
163163

164164
//send the header first

MongoConnectionString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void ParseString(string connectionString) {
8383
}
8484
}
8585
catch {
86-
throw new LameException("Couldn't use the connection string.");
86+
throw new InvalidMongoConnectionStringException();
8787
}
8888
}
8989

MongoDatabase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public MongoCollection GetCollection(string collection) {
431431
//verify the name is okay
432432
collection = (collection ?? string.Empty).Trim();
433433
if (string.IsNullOrEmpty(collection)) {
434-
throw new LameException("A collection must have a name!");
434+
throw new MissingCollectionNameException("collection");
435435
}
436436

437437
//check if this is already loaded
@@ -492,7 +492,7 @@ public MongoCursor GetLastCursor() {
492492

493493
//if there aren't any cursors throw an error
494494
if (this._Cursors.Count == 0) {
495-
throw new LameException("No cursors were found!");
495+
throw new NoCursorsFoundException();
496496
}
497497

498498
//return the most recently added item
@@ -527,7 +527,7 @@ public IEnumerable<MongoDocument> GetMore(MongoCursor cursor, int count) {
527527

528528
//make sure this is really a cursor
529529
if (cursor == null) {
530-
throw new LameException("This isn't a cursor!");
530+
throw new ArgumentNullException("cursor", "You must provide a cursor to use.");
531531
}
532532

533533
//start with the cursor value
@@ -567,7 +567,7 @@ public IEnumerable<MongoDocument> GetMore(MongoCursor cursor, int count) {
567567

568568
//make sure something was found
569569
if (response == null) {
570-
throw new LameException("Failed to get more records!");
570+
throw new MongoServerException("GetMore expected a response but nothing was returned from the server.");
571571
}
572572

573573
//return the found documents

MongoDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public void GenerateId() {
106106
public MongoOid Id {
107107
get { return this.Get<MongoOid>(Mongo.DocumentIdKey); }
108108
set {
109-
if (value == null && !(value is MongoOid)) {
110-
throw new LameException("Must be an Oid type!");
109+
if (value != null && !(value is MongoOid)) {
110+
throw new ArgumentException("You can only assign a MongoOid as the Id for a MongoDocument");
111111
}
112112
this.Set<MongoOid>(Mongo.DocumentIdKey, value);
113113
}

MongoOid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void SetId(string value) {
9090

9191
//verify the length
9292
if (value.Length != 24) {
93-
throw new LameException("Must be 24 characters... yeah...");
93+
throw new ArgumentException("The provided ID was not in an expected format.", "value");
9494
}
9595

9696
//parse every pair of bytes

0 commit comments

Comments
 (0)