mirror of
https://github.com/meineerde/rackstash.git
synced 2025-12-19 15:01: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:
parent
4eb381a733
commit
b228494d14
@ -10,6 +10,14 @@ require 'set'
|
||||
require 'rackstash/version'
|
||||
|
||||
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 = [
|
||||
DEBUG = 0,
|
||||
INFO = 1,
|
||||
|
||||
@ -82,15 +82,16 @@ module Rackstash
|
||||
|
||||
# Write a single log line to the log device.
|
||||
#
|
||||
# This method needs to be overwritten in child classes to write the
|
||||
# encoded log event to the adapter's device. By default, this method
|
||||
# raises a `NotImplementedError`.
|
||||
# This method needs to be overwritten in adapter sub classes to write the
|
||||
# encoded log event to the adapter's device. When not overwritten, this
|
||||
# method raises a {NotImplementedHereError}.
|
||||
#
|
||||
# @param log [Object] the encoded log event
|
||||
# @return [void]
|
||||
# @raise NotImplementedError
|
||||
# @raise NotImplementedHereError
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
@ -47,7 +47,8 @@ describe Rackstash::Adapters::Adapter do
|
||||
|
||||
describe '#write_single' 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
|
||||
|
||||
@ -8,6 +8,12 @@
|
||||
require 'spec_helper'
|
||||
|
||||
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
|
||||
expect(Rackstash::PROGNAME).to match %r{\Arackstash/v\d+(\..+)*\z}
|
||||
expect(Rackstash::PROGNAME).to be_frozen
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user