Skip to content

Commit

Permalink
Merge pull request #540 from cerebris/controller_errors
Browse files Browse the repository at this point in the history
Translate exceptions arrising in the ActsAsResourceController code in…
  • Loading branch information
lgebhardt committed Dec 8, 2015
2 parents bbd4138 + 4a02b37 commit 87abe81
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/jsonapi/acts_as_resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ def handle_exceptions(e)
case e
when JSONAPI::Exceptions::Error
render_errors(e.errors)
else # raise all other exceptions
# :nocov:
fail e
# :nocov:
else
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
render_errors(internal_server_error.errors)
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3270,4 +3270,13 @@ def test_get_namespaced_model_matching_resource
assert_response :success
assert_equal 'customers', json_response['data'][0]['type']
end
end

class Api::V7::CategoriesControllerTest < ActionController::TestCase
def test_uncaught_error_in_controller

get :show, {id: '1'}
assert_response 500
assert_match /Internal Server Error/, json_response['errors'][0]['detail']
end
end
20 changes: 20 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,15 @@ class SubSpecialError < PostsController::SpecialError; end
head :forbidden
end

def handle_exceptions(e)
case e
when PostsController::SpecialError
raise e
else
super(e)
end
end

#called by test_on_server_error
def self.set_callback_message(error)
@callback_message = "Sent from method"
Expand Down Expand Up @@ -758,6 +767,9 @@ class LineItemsController < JSONAPI::ResourceController
class OrderFlagsController < JSONAPI::ResourceController
end

class CategoriesController < JSONAPI::ResourceController
end

class ClientsController < JSONAPI::ResourceController
end
end
Expand Down Expand Up @@ -1406,6 +1418,14 @@ class ClientResource < JSONAPI::Resource
has_many :purchase_orders
end

class CategoryResource < CategoryResource
attribute :name

# Raise exception for failure in controller
def name
fail "Something Exceptional Happened"
end
end
end

module V8
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :customers
jsonapi_resources :purchase_orders
jsonapi_resources :line_items
jsonapi_resources :categories

jsonapi_resources :clients
end
Expand Down

0 comments on commit 87abe81

Please sign in to comment.