Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions lib/rage/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ def cable
end
# @!endgroup

# @!group Error Handler Configuration
# Allows configuring error handlers.
# @return [Rage::Configuration::ErrorHandlers]
def error_handlers
@error_handlers ||= ErrorHandlers.new
# @!group Error Reporter Configuration
# Allows configuring error reporters.
# @return [Rage::Configuration::ErrorReporters]
def error_reporters
@error_reporters ||= ErrorReporters.new
end
# @!endgroup

Expand Down Expand Up @@ -394,7 +394,7 @@ def validate_input!(obj)
end
end

class ErrorHandlers
class ErrorReporters
# @private
def initialize
@objects = []
Expand All @@ -405,46 +405,46 @@ def objects
@objects.dup
end

# Add a new error handler.
# Error handlers should respond to `#call` and accept one of:
# Add a new error reporter.
# Error reporters should respond to `#call` and accept one of:
# - `call(exception)`
# - `call(exception, context: {})`
#
# @param handler [#call]
# @param reporter [#call]
# @return [self]
# @example
# Rage.configure do
# config.error_handlers << SentryReporter.new
# config.error_reporters << SentryReporter.new
# end
def <<(handler)
validate_input!(handler)
return self if @objects.include?(handler)
def <<(reporter)
validate_input!(reporter)
return self if @objects.include?(reporter)

@objects << handler
Rage::Errors.__send__(:__register_reporter, handler)
@objects << reporter
Rage::Errors.__send__(:__register_reporter, reporter)

self
end

alias_method :push, :<<

# Remove an error handler.
# @param handler [#call] the handler to remove
# Remove an error reporter.
# @param reporter [#call] the reporter to remove
# @example
# handler = SentryReporter.new
# reporter = SentryReporter.new
# Rage.configure do
# config.error_handlers.delete(handler)
# config.error_reporters.delete(reporter)
# end
def delete(handler)
deleted = @objects.delete(handler)
Rage::Errors.__send__(:__unregister_reporter, handler) if deleted
def delete(reporter)
deleted = @objects.delete(reporter)
Rage::Errors.__send__(:__unregister_reporter, reporter) if deleted
deleted
end

private

def validate_input!(handler)
raise ArgumentError, "error handler must respond to #call" unless handler.respond_to?(:call)
def validate_input!(reporter)
raise ArgumentError, "error reporter must respond to #call" unless reporter.respond_to?(:call)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rage/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def report(exception, context: {})

# @private
def __register_reporter(reporter)
raise ArgumentError, "error handler must respond to #call" unless reporter.respond_to?(:call)
raise ArgumentError, "error reporter must respond to #call" unless reporter.respond_to?(:call)

reporter_id = @next_reporter_id
@next_reporter_id += 1
Expand Down
30 changes: 15 additions & 15 deletions spec/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@
described_class.instance_variable_set(:@next_reporter_id, original_next_reporter_id)
end

let(:error_handlers) { Rage::Configuration.new.error_handlers }
let(:error_reporters) { Rage::Configuration.new.error_reporters }

describe "configuration integration" do
it "is available via config.error_handlers" do
expect(Rage::Configuration.new.error_handlers).to be_an_instance_of(Rage::Configuration::ErrorHandlers)
it "is available via config.error_reporters" do
expect(Rage::Configuration.new.error_reporters).to be_an_instance_of(Rage::Configuration::ErrorReporters)
end

it "is available via Rage.errors" do
expect(Rage.errors).to eq(described_class)
end

it "requires error handlers to respond to #call" do
it "requires error reporters to respond to #call" do
expect {
error_handlers << Object.new
}.to raise_error(ArgumentError, "error handler must respond to #call")
error_reporters << Object.new
}.to raise_error(ArgumentError, "error reporter must respond to #call")
end

it "allows removing a registered error handler" do
it "allows removing a registered error reporter" do
call_count = 0
reporter = Class.new do
define_method(:call) { |_exception| call_count += 1 }
end.new

error_handlers << reporter
error_handlers.delete(reporter)
error_reporters << reporter
error_reporters.delete(reporter)

described_class.report(StandardError.new("test"))

Expand Down Expand Up @@ -65,7 +65,7 @@ def call(exception)
end.new

error = StandardError.new("test")
error_handlers << reporter
error_reporters << reporter
described_class.report(error, context: { user_id: 42 })

expect(reporter.exception).to be(error)
Expand All @@ -82,7 +82,7 @@ def call(exception, context: {})
end.new

error = StandardError.new("test")
error_handlers << reporter
error_reporters << reporter
described_class.report(error, context: { user_id: 42 })

expect(reporter.exception).to be(error)
Expand All @@ -101,7 +101,7 @@ def call(exception)
error = StandardError.new("test")
expect(error.backtrace).to be_nil

error_handlers << reporter
error_reporters << reporter
described_class.report(error)

expect(reporter.exception.backtrace).to be_an(Array)
Expand All @@ -124,8 +124,8 @@ def call(exception)
end.new

error = StandardError.new("test")
error_handlers << failed_reporter
error_handlers << successful_reporter
error_reporters << failed_reporter
error_reporters << successful_reporter

described_class.report(error)

Expand All @@ -140,7 +140,7 @@ def call(exception)
end.new

error = StandardError.new("test")
error_handlers << reporter
error_reporters << reporter

described_class.report(error)
described_class.report(error)
Expand Down