Skip to content

Loading…

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

Merged
merged 1 commit into from

2 participants

@gaurish

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
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 Ruby on Rails member

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.

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

@carlosantoniodasilva

Made the said changes & squashed commits :)

@carlosantoniodasilva
Ruby on Rails member

@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
@gaurish

@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 :smile:

@carlosantoniodasilva carlosantoniodasilva merged commit abb38fe into rails:master
@carlosantoniodasilva
Ruby on Rails member

No problem, thanks for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 20, 2012
  1. @gaurish

    Better Error handling when parsing database.yaml

    gaurish committed
    Provides a better error message incase the database.yaml
    has some errors.
Showing with 4 additions and 0 deletions.
  1. +4 −0 railties/lib/rails/application/configuration.rb
View
4 railties/lib/rails/application/configuration.rb
@@ -105,6 +105,10 @@ def threadsafe!
def database_configuration
require 'erb'
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. " \
+ "Error: #{e.message}"
end
def log_level
Something went wrong with that request. Please try again.