Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-1595

Improve error reporting when symbol/string is passed as read preference

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Trivial - P5 Trivial - P5
    • 2.19.0, 2.18.2
    • Affects Version/s: None
    • Component/s: Dev Exp
    • None
    • Environment:
      Ruby on Rails, ruby 2.5.1
    • 1
    • Fully Compatible

      If one mistakenly specifies read preference as a symbol in Mongoid, as follows:

      development:
        clients:
          default:
            database: mongoid
            hosts:
              - localhost:27100
            options:
              read: :primary
      

      Instead of the correct specification which uses a hash with mode subkey, as follows:

      development:
        clients:
          default:
            database: mongoid
            hosts:
              - localhost:27100
            options:
              read:
                mode: :primary
      

      The driver produces an error as follows:

      Mongo::Error::InvalidReadOption (Invalid read option: primary: must be a hash)
      

      The driver can in addition suggest a fix which is to replace the primitive value (:primary) with a hash (

      {mode: :primary}

      ).

      Test apps: https://github.com/p-mongo/tests/tree/master/mongoid-spec-rp-symbol & https://github.com/p-mongo/tests/tree/master/mongoid-spec-rp-hash.


      Original report:

      Here is a coding error. It is not related to the database, seem to be a driver only error.

      My server environment works but my RSpec tests don't work.

      1. See backtrace.png for the error. It's all about the mongo and mongoid gems. I tried with mongoid 6.1.1, 6.2.1 and 6.4.2. Although the error message is a bit different with 6.1.1.
      2. See init.png. The initialization is correct, at least that part the :read value which is a hash.
      3. Since 6.0.0, the read mode ("primary") is taken out of the hash. See read_mode.png
      4. Finally, the options :read value is no longer a hash (of course, it was taken out of it in step 3). This causes the exception we see in step 3. The validate call errors out.

       

       

        1. backtrace.png
          backtrace.png
          1.33 MB
        2. init.png
          init.png
          199 kB
        3. mongoid.yml
          5 kB
        4. read_mode.png
          read_mode.png
          198 kB
        5. Screen Shot 2018-11-16 at 5.04.17 PM.png
          Screen Shot 2018-11-16 at 5.04.17 PM.png
          74 kB
        6. Screen Shot 2018-11-16 at 5.08.53 PM.png
          Screen Shot 2018-11-16 at 5.08.53 PM.png
          572 kB
        7. Screen Shot 2018-11-16 at 5.08.53 PM.png
          Screen Shot 2018-11-16 at 5.08.53 PM.png
          572 kB
        8. Screen Shot 2018-11-16 at 5.15.22 PM.png
          Screen Shot 2018-11-16 at 5.15.22 PM.png
          12 kB
        9. validate.png
          validate.png
          247 kB

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            patrice4mongo Patrice Gagnon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: