1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-20 15:21:12 +00:00

Raise a custom Rackstash::NotImplementedHereError instead of NotImplementedError

Using the core `NotInmplementedError` is not desireable since its
documentation includes:

> Note that if `fork` raises a `NotImplementedError`, then
> `respond_to?(:fork)` returns `false`.

Since we are responding to the method but still raise an error, our
usage of the exception does not fulfill its documentation.

A custom error instead of a default `NoMethodError` is still desireable
since it significantly helps with debugging. With a different Exception,
we make it clear that a method is expected to be there and just wasn't
implemented by a subclass as opposed to the caller just using an object
wrong and calling entirely unexpected methods on it.
This commit is contained in:
Holger Just 2017-08-01 12:28:54 +02:00
parent 4eb381a733
commit b228494d14
4 changed files with 22 additions and 6 deletions

View File

@ -10,6 +10,14 @@ require 'set'
require 'rackstash/version' require 'rackstash/version'
module Rackstash module Rackstash
# A custom error which is raised by methods which need to be implemented
# elsewhere, usually in a subclass. Please refer to the documentation of the
# method which raised this error for details.
#
# Note that this error is not a `StandardError` and will not be rescued
# unless it or any of its ancestors, e.g. `Exception` is specified explicitly.
class NotImplementedHereError < ScriptError; end
SEVERITIES = [ SEVERITIES = [
DEBUG = 0, DEBUG = 0,
INFO = 1, INFO = 1,

View File

@ -82,15 +82,16 @@ module Rackstash
# Write a single log line to the log device. # Write a single log line to the log device.
# #
# This method needs to be overwritten in child classes to write the # This method needs to be overwritten in adapter sub classes to write the
# encoded log event to the adapter's device. By default, this method # encoded log event to the adapter's device. When not overwritten, this
# raises a `NotImplementedError`. # method raises a {NotImplementedHereError}.
# #
# @param log [Object] the encoded log event # @param log [Object] the encoded log event
# @return [void] # @return [void]
# @raise NotImplementedError # @raise NotImplementedHereError
def write_single(log) def write_single(log)
raise NotImplementedError, 'write needs to be implemented in a sub class' raise NotImplementedHereError, 'write_single needs to be implemented ' +
'in the actual adapter subclass'
end end
private private

View File

@ -47,7 +47,8 @@ describe Rackstash::Adapters::Adapter do
describe '#write_single' do describe '#write_single' do
it 'is not implemented in the abstract base class' do it 'is not implemented in the abstract base class' do
expect { adapter.write('something') }.to raise_error(NotImplementedError) expect { adapter.write('something') }
.to raise_error(Rackstash::NotImplementedHereError)
end end
end end
end end

View File

@ -8,6 +8,12 @@
require 'spec_helper' require 'spec_helper'
describe Rackstash do describe Rackstash do
describe Rackstash::NotImplementedHereError do
it 'inherits from ScriptError' do
expect(described_class.superclass).to equal ScriptError
end
end
it 'defines PROGRAME with the correct version' do it 'defines PROGRAME with the correct version' do
expect(Rackstash::PROGNAME).to match %r{\Arackstash/v\d+(\..+)*\z} expect(Rackstash::PROGNAME).to match %r{\Arackstash/v\d+(\..+)*\z}
expect(Rackstash::PROGNAME).to be_frozen expect(Rackstash::PROGNAME).to be_frozen