Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 1 commit into from

2 participants

Gaurish Sharma Carlos Antonio da Silva
Gaurish Sharma

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

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}"

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 Sharma

@carlosantoniodasilva

Made the said changes & squashed commits :)

Carlos Antonio da Silva

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

Gaurish Sharma gaurish Better Error handling when parsing database.yaml
Provides a better error message incase the database.yaml
has some errors.
61425f2
Gaurish Sharma

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

Carlos Antonio da Silva carlosantoniodasilva merged commit abb38fe into from
Carlos Antonio da Silva

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 Sharma

    Better Error handling when parsing database.yaml

    gaurish authored
    Provides a better error message incase the database.yaml
    has some errors.
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 0 deletions.
  1. +4 −0 railties/lib/rails/application/configuration.rb
4 railties/lib/rails/application/configuration.rb
View
@@ -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.