Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-135

MongoCommandException doesn't contain sufficient information to understand what happened

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.0
    • Affects Version/s: 0.9
    • Component/s: None
    • None
    • Environment:
      Win 7

      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;
      }

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            ianmercer Ian Mercer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: