Skip to content

Commit

Permalink
Merge pull request #577 from rwz/3-2-stable
Browse files Browse the repository at this point in the history
Strengthened middleware to gracefully handle errors
  • Loading branch information
bcardarella committed Aug 13, 2013
2 parents 92f4e45 + 15be86a commit 6ad3bdd
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/client_side_validations/middleware.rb
Expand Up @@ -11,16 +11,32 @@ def initialize(app)
end

def call(env)
if matches = /^\/validators\/(\w+)$/.match(env['PATH_INFO'])
if ClientSideValidations::Config.disabled_validators.include?(matches[1].to_sym)
[500, {'Content-Type' => 'application/json', 'Content-Length' => '0'}, ['']]
else
"::ClientSideValidations::Middleware::#{matches[1].camelize}".constantize.new(env).response
end
if matches = /\A\/validators\/(\w+)\z/.match(env['PATH_INFO'])
process_request(matches.captures.first, env)
else
@app.call(env)
end
end

def process_request(validation, env)
if disabled_validators.include?(validation)
error_resp
else
klass_name = validation.camelize
klass_name = "::ClientSideValidations::Middleware::#{klass_name}"
klass_name.constantize.new(env).response
end
rescue => e
error_resp
end

def disabled_validators
ClientSideValidations::Config.disabled_validators.map(&:to_s)
end

def error_resp
[500, {'Content-Type' => 'application/json', 'Content-Length' => '0'}, ['']]
end
end

class Base
Expand Down Expand Up @@ -109,8 +125,8 @@ def convert_scope_value_from_null_to_nil

def extract_resources
parent_key = (request.params.keys - IGNORE_PARAMS).first
if nested?(request.params[parent_key], 1)

if nested?(request.params[parent_key], 1)
klass, attribute, value = uproot(request.params[parent_key])
klass = klass.classify.constantize
else
Expand Down

0 comments on commit 6ad3bdd

Please sign in to comment.