Improved Error handling when parsing database.yaml, Fixes #8143 #8279

Merged
merged 1 commit into from Nov 20, 2012

Projects

None yet

2 participants

@gaurish
Contributor
gaurish commented Nov 20, 2012

Provides better error message incase the database.yaml has some errors. fixes #8143

error message before the change, incase database.yml is invalid:

$ rails c
/home/gaurish/.rvm/rubies/ruby-1.9.3-p286-perf/lib/ruby/1.9.1/psych.rb:203:in `parse': (<unknown>): mapping values are not allowed in this context at line 2 column 10 (Psych::SyntaxError)     

Error message AFTER the change, incase database.yml is invalid:

$ rails c
/home/gaurish/code/repo/rails/railties/lib/rails/application/configuration.rb:112:in `rescue in database_configuration': YAML syntax error occurred while parsing /tmp/db/config/database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed Exact Error: (<unknown>): mapping values are not allowed in this context at line 2 column 10 (RuntimeError)

cc: @carlosantoniodasilva, @steveklabnik

@carlosantoniodasilva carlosantoniodasilva commented on an outdated diff Nov 20, 2012
railties/lib/rails/application/configuration.rb
@@ -104,7 +104,13 @@ def threadsafe!
# YAML::load.
def database_configuration
require 'erb'
- YAML.load ERB.new(IO.read(paths["config/database"].first)).result
+ begin
+ YAML.load ERB.new(IO.read(paths["config/database"].first)).result
+ rescue Psych::SyntaxError => e
+ raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " \
+ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
+ "Exact Error: #{e.message}"
carlosantoniodasilva
carlosantoniodasilva Nov 20, 2012 Owner

I don't think Exact is necessary here. Also, I think you can use the rescue method syntax instead of begin..rescue..end in this case.

Contributor
gaurish commented Nov 20, 2012

@carlosantoniodasilva
Made the said changes & squashed commits :)

@gaurish thanks. Can you unindent the lines please? rescue aligns with def..end in this case, like in 3b99653

@gaurish gaurish Better Error handling when parsing database.yaml
Provides a better error message incase the database.yaml
has some errors.
61425f2
Contributor
gaurish commented Nov 20, 2012

@carlosantoniodasilva
Done!

Btw, thanks for taking time to review this. Hopefully in future nobody else would waste so much time over such a silly error 😄

@carlosantoniodasilva carlosantoniodasilva merged commit abb38fe into rails:master Nov 20, 2012

No problem, thanks for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment