Revision 0f7d219a
Added by Daniel Lobato Garcia over 10 years ago
app/controllers/api/base_controller.rb | ||
---|---|---|
|
||
rescue_from StandardError, :with => lambda { |error|
|
||
logger.error "#{error.message} (#{error.class})\n#{error.backtrace.join("\n")}"
|
||
render_error 'standard_error', :status => 500, :locals => { :exception => error }
|
||
render_error 'standard_error', :status => :internal_server_error, :locals => { :exception => error }
|
||
}
|
||
|
||
rescue_from ScopedSearch::QueryNotSupported,
|
app/controllers/api/v1/home_controller.rb | ||
---|---|---|
|
||
def status
|
||
end
|
||
|
||
def route_error
|
||
render_error 'route_error', :status => :not_found
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
app/controllers/api/v2/base_controller.rb | ||
---|---|---|
end
|
||
end
|
||
|
||
def render_error(error, options = { })
|
||
render options.merge(:template => "api/v2/errors/#{error}",
|
||
:layout => 'api/v2/layouts/error_layout')
|
||
end
|
||
end
|
||
end
|
||
end
|
app/controllers/api/v2/home_controller.rb | ||
---|---|---|
|
||
def status
|
||
end
|
||
|
||
def route_error
|
||
render_error 'route_error', :status => :not_found
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
app/views/api/v1/errors/route_error.json.rabl | ||
---|---|---|
object false
|
||
|
||
node(:message) { 'Not found' }
|
||
|
app/views/api/v2/errors/route_error.json.rabl | ||
---|---|---|
object false
|
||
|
||
node(:message) { 'Not found' }
|
||
|
app/views/api/v2/layouts/error_layout.json.erb | ||
---|---|---|
{
|
||
"error": <%= yield %>
|
||
}
|
app/views/api/v2/layouts/index_layout.json.erb | ||
---|---|---|
"order": <%= metadata_order.to_json.html_safe %>
|
||
},
|
||
"<%= "#{root_node_name}" %>": <%= yield %>
|
||
}
|
||
}
|
config/routes/test.rb | ||
---|---|---|
if Rails.env.test?
|
||
Foreman::Application.routes.draw do
|
||
namespace :api do
|
||
resources :testable, :only => :index
|
||
resources :testable, :only => :index do
|
||
get :raise_error, :on => :collection
|
||
end
|
||
end
|
||
end
|
||
end
|
test/functional/api/base_controller_subclass_test.rb | ||
---|---|---|
def index
|
||
render :text => 'dummy', :status => 200
|
||
end
|
||
|
||
def raise_error
|
||
render_error 'standard_error', :status => :internal_server_error,
|
||
:locals => { :exception => StandardError }
|
||
end
|
||
end
|
||
|
||
class Api::TestableControllerTest < ActionController::TestCase
|
||
... | ... | |
assert_not session[:user]
|
||
end
|
||
end
|
||
|
||
context 'errors' do
|
||
test "top level key is error, no metadata included" do
|
||
get :raise_error
|
||
assert_equal ['error'], ActiveSupport::JSON.decode(@response.body).keys
|
||
end
|
||
end
|
||
end
|
test/functional/api/v2/hosts_controller_test.rb | ||
---|---|---|
facts = fact_json['facts']
|
||
post :facts, {:name => hostname, :facts => facts}, set_session_user
|
||
assert_response :unprocessable_entity
|
||
assert_equal 'A stub failure', JSON.parse(response.body)['errors']['foo'].first
|
||
assert_equal 'A stub failure', JSON.parse(response.body)['error']['errors']['foo'].first
|
||
end
|
||
|
||
context 'BMC proxy operations' do
|
test/functional/api/v2/users_controller_test.rb | ||
---|---|---|
assert_response :forbidden
|
||
|
||
response = ActiveSupport::JSON.decode(@response.body)
|
||
assert response['details'] == "You are trying to delete your own account"
|
||
assert response['message'] == "Access denied"
|
||
assert_equal "You are trying to delete your own account", response['error']['details']
|
||
assert_equal "Access denied", response['error']['message']
|
||
assert User.exists?(user)
|
||
end
|
||
end
|
test/lib/foreman/access_permissions_test.rb | ||
---|---|---|
"apipie/apipies/index",
|
||
|
||
# API app controller stub
|
||
"api/testable/index"
|
||
"api/testable/index", "api/testable/raise_error"
|
||
]
|
||
|
||
MAY_SKIP_AUTHORIZED = [ "about/index" ]
|
Also available in: Unified diff
fixes #3960 - wrap APIv2 errors in an "error" node