private static void AddErrorLabelsFromCommandResult(MongoCommandException exception, BsonDocument result)
 {
     // note: make a best effort to extract the error labels from the result, but never throw an exception
     if (result != null)
     {
         BsonValue errorLabels;
         if (result.TryGetValue("errorLabels", out errorLabels) && errorLabels.IsBsonArray)
         {
             foreach (var errorLabel in errorLabels.AsBsonArray)
             {
                 if (errorLabel.IsString)
                 {
                     exception.AddErrorLabel(errorLabel.AsString);
                 }
             }
         }
     }
 }
        public void Serialization_should_work()
        {
            var subject = new MongoCommandException(_connectionId, _message, _command, _commandResult);

            subject.AddErrorLabel("one");

            var formatter = new BinaryFormatter();

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, subject);
                stream.Position = 0;
                var rehydrated = (MongoCommandException)formatter.Deserialize(stream);

                rehydrated.ErrorLabels.Should().Equal(subject.ErrorLabels);
                rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
                rehydrated.Message.Should().Be(_message);
                rehydrated.InnerException.Should().BeNull();
                rehydrated.Command.Should().Be(_command);
                rehydrated.Result.Should().Be(_commandResult);
            }
        }