X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f88280e6c45755b9df41af3cc88d623a07d864c6..dce1c6512d7c46e68e2c83b7a1a9d484a19edac3:/app/controllers/application_controller.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 96c95592ec..493db3cc1b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,35 @@ class ApplicationController < ActionController::Base before_filter :uncamelcase_params_hash_keys before_filter :find_object_by_uuid, :except => :index + unless Rails.application.config.consider_all_requests_local + rescue_from Exception, + :with => :render_error + rescue_from ActiveRecord::RecordNotFound, + :with => :render_not_found + rescue_from ActionController::RoutingError, + :with => :render_not_found + rescue_from ActionController::UnknownController, + :with => :render_not_found + rescue_from ActionController::UnknownAction, + :with => :render_not_found + end + + def render_error(e) + logger.error e.inspect + logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace + if @object and @object.errors and @object.errors.full_messages + errors = @object.errors.full_messages + else + errors = [e.inspect] + end + render json: { errors: errors }, status: 422 + end + + def render_not_found(e=ActionController::RoutingError.new("Path not found")) + logger.error e.inspect + render json: { errors: ["Path not found"] }, status: 401 + end + def index @objects ||= model_class.all render_list @@ -27,7 +56,7 @@ class ApplicationController < ActionController::Base def update @attrs = params[resource_name] - if @attrs.class == String + if @attrs.is_a? String @attrs = uncamelcase_hash_keys(JSON.parse @attrs) end @object.update_attributes @attrs @@ -45,19 +74,32 @@ class ApplicationController < ActionController::Base end def find_object_by_uuid - logger.info params.inspect if params[:id] and params[:id].match /\D/ params[:uuid] = params.delete :id end @object = model_class.where('uuid=?', params[:uuid]).first end + def self.accept_attribute_as_json(attr, force_class=nil) + before_filter lambda { accept_attribute_as_json attr, force_class } + end + def accept_attribute_as_json(attr, force_class) + if params[resource_name].is_a? Hash + if params[resource_name][attr].is_a? String + params[resource_name][attr] = JSON.parse params[resource_name][attr] + if force_class and !params[resource_name][attr].is_a? force_class + raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}") + end + end + end + end + def uncamelcase_params_hash_keys self.params = uncamelcase_hash_keys(params) end - def uncamelcase_hash_keys(h) - if h.is_a? Hash + def uncamelcase_hash_keys(h, max_depth=-1) + if h.is_a? Hash and max_depth != 0 nh = Hash.new h.each do |k,v| if k.class == String @@ -67,7 +109,7 @@ class ApplicationController < ActionController::Base else nk = k end - nh[nk] = uncamelcase_hash_keys(v) + nh[nk] = uncamelcase_hash_keys(v, max_depth-1) end h.replace(nh) end