[CSHARP-135] MongoCommandException doesn't contain sufficient information to understand what happened Created: 14/Dec/10  Updated: 02/Apr/15  Resolved: 14/Dec/10

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 0.9
Fix Version/s: 1.0

Type: Improvement Priority: Minor - P4
Reporter: Ian Mercer Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win 7



 Description   

When a MongoCommandException exception is thrown the code that reports it discards the 'assertion' and 'assertion code' from the results object. These typically contain the actual reason for the problem.

Suggestions:-

1) Maybe the ErrorMessage property of the CommandResult class should be changed to be a concatenation of the errmsg, assertion code, and assertion properties as the error message itself is fairly useless

2) The RunCommandAs method should be changed to use the existing constructor for MongoCommandException that puts the result object into the .Data property of the exception. (Most developers however aren't familiar with Exception.Data and might miss this)

3) if not #1 then at least the RunCommandAs should concatenate the strings when it creates an exception message.

public TCommandResult RunCommandAs<TCommandResult>(
IMongoCommand command
) where TCommandResult : CommandResult {
var result = CommandCollection.FindOneAs<TCommandResult>(command);
if (!result.Ok) {
if (result.ErrorMessage == "not master")

{ server.Disconnect(); }

string errorMessage = string.Format("Command failed:

{0}

", string.Join(" ", result.Select(x => x.ToString()).ToArray()); // a simple dump of the result enumeration
throw new MongoCommandException(errorMessage, result);
}
return result;
}



 Comments   
Comment by Robert Stam [ 14/Dec/10 ]

Implemented more or less as suggested. The additional information in the error message includes the command response in JSON format. Also added a CommandResult property to MongoCommandException so it's not buried in the little known Data property. The MongoCommandResult class now includes the original Command object which is helpful when troubleshooting command failures.

Generated at Wed Feb 07 21:35:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.