Skip to content

Commit

Permalink
Add ability to supply a callable error handler
Browse files Browse the repository at this point in the history
resolves #23

Why?
The scim engine returns a custom response to the caller in the event
that there is a 500 error in the engine. It rescues any standard error
in order to do this.

Unfortunately this means that when something is broken it does not
bubble up to the parent app's error handling system or even be printed
in the logs.

We cannot just re-raise the exception because you cannot return a
response AND raise an exception as a part of the same request.

To help, this PR will make is possible for you to supply a callable
object that take the exception as it's argument.

If no callable object is provided, we will output the exception to the
logs so that silence is not the default.

To get the old behavior of completely ignoring an exception, you could
supply an empty proc.
  • Loading branch information
rreinhardt9 committed May 29, 2020
1 parent b6f156e commit 7ba7da5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ spec/dummy/db/*.sqlite3
spec/dummy/db/*.sqlite3-journal
spec/dummy/log/*.log
spec/dummy/tmp/
.rubocop-https*
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inherit_from:
- https://raw.githubusercontent.com/lessonly/rubocop-default-configuration/master/.rubocop.yml
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Upcoming Release

- Any unhandled error is now logged to the configured rails logger by default. You can also now supply a custom callable that will be used to handle those exceptions instead.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ Sample request:
$ curl -X PATCH 'http://username:password@localhost:3000/scim/v2/Users/1' -d '{"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], "Operations": [{"op": "replace", "value": { "active": false }}]}' -H 'Content-Type: application/scim+json'
```

### Error Handling

By default, scim_rails will output any unhandled exceptions to your configured rails logs.

If you would like, you can supply a custom handler for exceptions in the initializer. The only requirement is that the value you supply responds to `#call`.

For example, you might want to notify Honeybadger:

```ruby
ScimRails.configure do |config|
config.on_error = ->(e) { Honeybadger.notify(e) }
end
```

## Contributing

### [Code of Conduct](https://github.com/lessonly/scim_rails/blob/master/CODE_OF_CONDUCT.md)
Expand Down
13 changes: 8 additions & 5 deletions app/controllers/concerns/scim_rails/exception_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class UnsupportedPatchRequest < StandardError
end

included do
# StandardError must be ordered _first_ or it will catch all exceptions
#
# TODO: Build a plugin/configuration for error handling so that the
# detailed production errors are logged somewhere if desired.
if Rails.env.production?
rescue_from StandardError do
rescue_from StandardError do |exception|
on_error = ScimRails.config.on_error
if on_error.respond_to?(:call)
on_error.call(exception)
else
Rails.logger.error(exception.inspect)
end

json_response(
{
schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
Expand Down
16 changes: 11 additions & 5 deletions lib/scim_rails/config.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
# frozen_string_literal: true

module ScimRails
class << self
def configure
yield config
end

def config
@_config ||= Config.new
@config ||= Config.new
end
end

# Class containing configuration of ScimRails
class Config
ALGO_NONE = "none".freeze
ALGO_NONE = "none"

attr_accessor \
attr_writer \
:basic_auth_model,
:mutable_user_attributes_schema,
:scim_users_model

attr_accessor \
:basic_auth_model_authenticatable_attribute,
:basic_auth_model_searchable_attribute,
:mutable_user_attributes,
:mutable_user_attributes_schema,
:on_error,
:queryable_user_attributes,
:scim_users_list_order,
:scim_users_model,
:scim_users_scope,
:scim_user_prevent_update_on_create,
:signing_secret,
Expand Down

0 comments on commit 7ba7da5

Please sign in to comment.