Skip to content

Commit

Permalink
Merge pull request #10 from Kaligo/feature/event-source-error-handler
Browse files Browse the repository at this point in the history
Add error_handler configuration option to the EventSource module
  • Loading branch information
Drenmi authored Mar 26, 2021
2 parents 33da0f9 + fab55a1 commit 9a2164c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.5.5

### New features

- Add `error_handler` configuration option to the `EventSource` module.

## 0.5.4

### New features
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
stimpack (0.5.4)
stimpack (0.5.5)
activesupport (~> 6.1)

GEM
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and behaviour.
## Table of Contents

- [EventSource](#eventsource)
- [Error handling](#error-handling)
- [FunctionalObject](#functionalobject)
- [OptionsDeclaration](#optionsdeclaration)
- [ResultMonad](#resultmonad)
Expand Down Expand Up @@ -67,6 +68,21 @@ rescue StandardError => error
end
```

Alternatively, you can configure an event handler for all `EventSource` classes
to use. The error handler needs to respond fo `#call` and will be passed a
single argument, the error that was raised:

**Example:**

```ruby
EventSource.error_handler = ->(error) { AppSignal.error(error) }
```

This can be useful for instrumentation, and to handle errors differently
depending on which environment the code is running in.

*Note: Once configured, this will apply to all listeners.*

## FunctionalObject

A simple mixin that provides a shorthand notation for instantiating and
Expand Down
10 changes: 10 additions & 0 deletions lib/stimpack/event_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ module Stimpack
# Checkout.on(:error) { |event| Appsignal.report(event.message) }
#
module EventSource
DEFAULT_ERROR_HANDLER = ->(_error) {}

def self.error_handler
@error_handler || DEFAULT_ERROR_HANDLER
end

def self.error_handler=(handler)
@error_handler = handler
end

module ClassMethods
# Callback registry that stores a mapping of callbacks for all concrete
# service classes. The registry is a hash that lives in the base class,
Expand Down
2 changes: 2 additions & 0 deletions lib/stimpack/event_source/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def initialize(raise_errors:, &block)
def call(...)
block.(...)
rescue StandardError => e
EventSource.error_handler.(e)

raise e if raise_errors
end

Expand Down
2 changes: 1 addition & 1 deletion lib/stimpack/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Stimpack
VERSION = "0.5.4"
VERSION = "0.5.5"
end
20 changes: 20 additions & 0 deletions spec/stimpack/event_source/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
subject(:listener) { described_class.new(raise_errors: raise_errors, &block) }

let(:raise_errors) { false }
let(:error_handler) { instance_spy(Proc) }

before do
Stimpack::EventSource.error_handler = error_handler
end

after do
Stimpack::EventSource.error_handler = nil
end

describe "#call" do
context "when no errors are raised" do
Expand All @@ -22,13 +31,24 @@
end

it { expect(receiver).to have_received(:foo).once }
it { expect(error_handler).not_to have_received(:call) }
end

context "when errors are raised" do
let(:block) do
proc { raise StandardError }
end

context "when an error handler is configured" do
let(:raise_errors) { false }

before do
listener.()
end

it { expect(error_handler).to have_received(:call).once.with(StandardError) }
end

context "when configured to raise errors" do
let(:raise_errors) { true }

Expand Down
20 changes: 20 additions & 0 deletions spec/stimpack/event_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ def self.to_s
end
end

describe ".error_handler" do
context "when no error handler has been configured" do
it { expect(described_class.error_handler).to respond_to(:call) }
end

context "when an error handler has been configured" do
let(:error_handler) { instance_double(Proc) }

before do
described_class.error_handler = error_handler
end

after do
described_class.error_handler = nil
end

it { expect(described_class.error_handler).to eq(error_handler) }
end
end

describe ".on" do
it { expect { service.on(:foo) {} }.to change { klass.event_listeners["Foo.foo"].size }.by(1) }
end
Expand Down

0 comments on commit 9a2164c

Please sign in to comment.