Skip to content

Commit 834e9d3

Browse files
committed
Enable exception superclasses to be whitelisted.
This makes it easy to reraise any type of exception (or groups of exception classes). I needed this for integration with a application monitoring service.
1 parent 4f2e459 commit 834e9d3

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ The relationship methods (`relationship`, `has_one`, and `has_many`) support the
270270

271271
`to_one` relationships support the additional option:
272272
* `foreign_key_on` - defaults to `:self`. To indicate that the foreign key is on the related resource specify `:related`.
273-
273+
274274
Examples:
275275

276276
```ruby
@@ -338,7 +338,7 @@ class ContactResource < JSONAPI::Resource
338338
end
339339
```
340340

341-
Then a request could pass in a filter for example `http://example.com/contacts?filter[name_last]=Smith` and the system
341+
Then a request could pass in a filter for example `http://example.com/contacts?filter[name_last]=Smith` and the system
342342
will find all people where the last name exactly matches Smith.
343343

344344
##### Default Filters
@@ -1276,8 +1276,9 @@ JSONAPI.configure do |config|
12761276
# processing. If you want to use Rails' `rescue_from` macro to
12771277
# catch this error and render a 403 status code, you should add
12781278
# the `Pundit::NotAuthorizedError` to the `exception_class_whitelist`.
1279+
# Subclasses of the whitelisted classes will also be whitelisted.
12791280
config.exception_class_whitelist = []
1280-
1281+
12811282
# Resource Linkage
12821283
# Controls the serialization of resource linkage for non compound documents
12831284
# NOTE: always_include_has_many_linkage_data is not currently implemented

lib/jsonapi/active_record_operations_processor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def process_operation(operation)
2929
raise e
3030

3131
rescue => e
32-
if JSONAPI.configuration.exception_class_whitelist.include?(e.class)
32+
if JSONAPI.configuration.exception_class_whitelist.any? { |k| e.class.ancestors.include?(k) }
3333
raise e
3434
else
3535
internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)

test/fixtures/active_record.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class ErrorRaisingOperationsProcessor < ActiveRecordOperationsProcessor
429429
def process_operation(operation)
430430
mock_operation = Minitest::Mock.new
431431
mock_operation.expect(:apply, true) do
432-
raise PostsController::SpecialError
432+
raise PostsController::SubSpecialError
433433
end
434434
super(mock_operation)
435435
end
@@ -445,6 +445,7 @@ class PeopleController < JSONAPI::ResourceController
445445
class PostsController < ActionController::Base
446446
include JSONAPI::ActsAsResourceController
447447
class SpecialError < StandardError; end
448+
class SubSpecialError < PostsController::SpecialError; end
448449

449450
# This is used to test that classes that are whitelisted are reraised by
450451
# the operations processor.

0 commit comments

Comments
 (0)