Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Obscure Error with invalid YAML in database.yml #8143

Closed
gaurish opened this Issue Nov 8, 2012 · 8 comments

Comments

Projects
None yet
3 participants
Contributor

gaurish commented Nov 8, 2012

My app wouldn’t start with a very obscure error:

psych.rb:203:in `parse': (<unknown>): mapping values are not allowed in this context at line 18 column 11 (Psych::SyntaxError)

The full stack trace looked like this:

$ bundle exec rails s
=> Booting Thin
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych.rb:203:in `parse': (<unknown>): mapping values are not allowed in this context at line 18 column 11 (Psych::SyntaxError)
        from /home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych.rb:203:in `parse_stream'
        from /home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych.rb:151:in `parse'
        from /home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/psych.rb:127:in `load'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/application/configuration.rb:115:in `database_configuration'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railtie.rb:78:in `block (2 levels) in <class:Railtie>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:42:in `each'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/base.rb:721:in `<top (required)>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise/orm/active_record.rb:3:in `<top (required)>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
        from /home/gaurish/Dropbox/code/practice/myapp/config/initializers/devise.rb:16:in `block in <top (required)>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/lib/devise.rb:247:in `setup'
        from /home/gaurish/Dropbox/code/practice/myapp/config/initializers/devise.rb:3:in `<top (required)>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `block in load'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/engine.rb:588:in `block (2 levels) in <class:Engine>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/engine.rb:587:in `each'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/engine.rb:587:in `block in <class:Engine>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `instance_exec'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `run'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `each'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `run_initializers'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/application.rb:136:in `initialize!'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/railtie/configurable.rb:30:in `method_missing'
        from /home/gaurish/Dropbox/code/practice/myapp/config/environment.rb:5:in `<top (required)>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
        from /home/gaurish/Dropbox/code/practice/myapp/config.ru:4:in `block in <main>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
        from /home/gaurish/Dropbox/code/practice/myapp/config.ru:1:in `new'
        from /home/gaurish/Dropbox/code/practice/myapp/config.ru:1:in `<main>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/server.rb:46:in `app'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/server.rb:70:in `start'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:55:in `block in <top (required)>'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:50:in `tap'
        from /home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:50:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

As you may notice this is not really informative as it doesn't give me any clue in which file I have misformatted YAML code. So, After spending some time randomly changing few thing here & there, I discovered the error was with my database.yml. so to reproduce this error, just place the following in your database.yml

development: &defaults
  adapter: postgresql
  database: myapp_development
  username: gaurish
  password: ""
  encoding: unicode
test:
  <<:*defaults
  database: myapp_test

the Psych::SyntaxError exception should be captured & an helpful error message that informs which file the error occurred in should be shown instead.

$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ rails -v
Rails 3.2.8

Your yaml file is invalid:

development: &defaults
  adapter: postgresql
  database: myapp_development
  username: gaurish
  password: ""
  encoding: unicode
test:
  <<:*defaults
  database: myapp_test
$ irb -ryaml
>> YAML.load_file 'x.yml'
Psych::SyntaxError: (x.yml): mapping values are not allowed in this context at line 9 column 11
    from ~/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/psych.rb:203:in `parse'
....

You need to add a space with before the *defaults, otherwise it 💥:

development: &defaults
  adapter: postgresql
  database: myapp_development
  username: gaurish
  password: ""
  encoding: unicode
test:
  <<: *defaults
  database: myapp_test
$ irb -ryaml
>> YAML.load_file 'x.yml'
=> {"development"=>{"adapter"=>"postgresql", "database"=>"myapp_development", "username"=>"gaurish", "password"=>"", "encoding"=>"unicode"}, "test"=>{"adapter"=>"postgresql", "database"=>"myapp_test", "username"=>"gaurish", "password"=>"", "encoding"=>"unicode"}}

If you want to improve the error, please feel free to send a pull request, thanks!

Member

steveklabnik commented Nov 8, 2012

If anyone would like to improve this error message, feel free to submit a pull request making it better. This isn't really a bug, though, so I'm giving it a close.

@steveklabnik steveklabnik reopened this Nov 8, 2012

Member

steveklabnik commented Nov 8, 2012

Ah! Darn GitHub, @carlosantoniodasilva closed it just as I clicked the button, which changed to 'open' :p

@steveklabnik ops, sorry :D

Contributor

gaurish commented Nov 8, 2012

Yeah, I know its invalid YAML. But the error message doesn't point to file which contains error; thus making it almost impossible to debug. This according to me is a bug.

Anyways, I would like to submit a PR to improve this error message. What part of codebase handles this? Which file I should be editing?

@gaurish I'd say this line gives you a hint about where the error might come from:

railties-3.2.8/lib/rails/application/configuration.rb:115:in `database_configuration'

You can start by there I think.

Contributor

gaurish commented Nov 8, 2012

will look this up over the weekend & will try to submit a patch. Thanks

No problem :)

carlosantoniodasilva added a commit that referenced this issue Nov 20, 2012

Merge pull request #8279 from gaurish/database_error
Improved Error handling when parsing database.yaml, Fixes #8143
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment