Project

General

Profile

« Previous | Next » 

Revision 0a801ca0

Added by Ohad Levy over 12 years ago

  • ID 0a801ca08e85501de62bbe6ab6b09b7a418b0188

fixes #1146 - Provide error details via the API upon failure

View differences:

app/controllers/application_controller.rb
rescue_from ActionController::RoutingError, :with => :no_puppetclass_documentation_handler
rescue_from ScopedSearch::QueryNotSupported, :with => :invalid_search_query
rescue_from Exception, :with => :generic_exception
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
# standard layout to all controllers
helper 'layout'
......
end
def process_error hash = {}
hash[:object] ||= eval("@#{controller_name.singularize}")
hash[:object] ||= eval("@#{controller_name.singularize}")
case action_name
when "create" then hash[:render] ||= "new"
when "update" then hash[:render] ||= "edit"
when "destroy" then
hash[:redirect] ||= eval("#{controller_name}_url")
hash[:error_msg] ||= hash[:object].errors.full_messages.join("<br/>")
when "create" then hash[:render] ||= "new"
when "update" then hash[:render] ||= "edit"
when "destroy" then
hash[:redirect] ||= eval("#{controller_name}_url")
end
hash[:error_msg] ||= hash[:object].errors.full_messages
hash[:error_msg] = [hash[:error_msg]].flatten
hash[:json_code] ||= :unprocessable_entity
logger.info "Failed to save: #{hash[:error_msg].join(", ")}"
respond_to do |format|
format.html do
error hash[:error_msg] if hash[:error_msg]
render :action => hash[:render] if hash[:render]
redirect_to hash[:redirect] if hash[:redirect]
return
end
format.json { render :json => hash[:object].errors, :status => hash[:json_code]}
format.html do
error hash[:error_msg].join("<br/>") unless hash[:error_msg].empty?
render :action => hash[:render] if hash[:render]
redirect_to hash[:redirect] if hash[:redirect]
return
end
format.json { render :json => {"errors" => hash[:object].errors} , :status => hash[:json_code]}
end
end
def redirect_back_or_to url
redirect_to request.referer.empty? ? url : :back
end
app/controllers/hosts_controller.rb
if @host.setBuild != false
process_success :success_msg => "Enabled #{@host.name} for rebuild on next boot", :success_redirect => :back
else
process_error :redirect => :back, :error_msg => "Failed to enable #{@host.name} for installation: #{@host.errors.full_messages.join("br")}"
process_error :redirect => :back, :error_msg => ["Failed to enable #{@host.name} for installation", @host.errors.full_messages]
end
end

Also available in: Unified diff