1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Improve error messages for ClassRegistry

This commit is contained in:
Holger Just 2020-07-10 17:46:56 +02:00
parent eaa13c8111
commit 76731b1d77
2 changed files with 10 additions and 9 deletions

View File

@ -27,7 +27,7 @@ module Rackstash
# Retrieve the registered class for a given name. If the argument is already
# a class, we return it unchanged.
#
# @param spec [Class,String,Symbol] Either a class (in which case it is
# @param spec [Class, String, Symbol] Either a class (in which case it is
# returned directly) or the name of a registered class.
# @raise [TypeError] when giving an invalid object
# @return [Class, nil] the registered class (when giving a `String` or
@ -41,7 +41,7 @@ module Rackstash
# Retrieve the registered class for a given name. If the argument is already
# a class, we return it unchanged.
#
# @param spec [Class,String,Symbol] either a class (in which case it is
# @param spec [Class, String, Symbol] either a class (in which case it is
# returned directly) or the name of a registered class
# @param default [Object] the default value that is returned if no
# registered class could be found for the given `spec` and no block was
@ -63,11 +63,11 @@ module Rackstash
next yield(key) if block_given?
next default unless UNDEFINED.equal? default
raise KeyError, "No #{@object_type} was registered for #{spec.inspect}"
raise KeyError, "No #{object_type} was registered for #{spec.inspect}"
end
else
raise TypeError, "#{spec.inspect} can not be used to describe " \
"#{@object_type} classes"
"#{object_type} classes"
end
end
@ -88,7 +88,8 @@ module Rackstash
when String, Symbol
@registry[name.to_sym] = registered_class
else
raise TypeError, "Can not use #{name.inspect} to register a #{@object_type} class"
raise TypeError, "Can not use #{name.inspect} to register " \
"#{object_type} classes"
end
registered_class
end

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
#
# Copyright 2017 - 2018 Holger Just
# Copyright 2017 - 2020 Holger Just
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
@ -99,11 +99,11 @@ RSpec.describe Rackstash::ClassRegistry do
it 'rejects invalid names' do
expect { registry[0] = Class.new }
.to raise_error(TypeError, 'Can not use 0 to register a value class')
.to raise_error(TypeError, 'Can not use 0 to register value classes')
expect { registry[nil] = Class.new }
.to raise_error(TypeError, 'Can not use nil to register a value class')
.to raise_error(TypeError, 'Can not use nil to register value classes')
expect { registry[String] = Class.new }
.to raise_error(TypeError, 'Can not use String to register a value class')
.to raise_error(TypeError, 'Can not use String to register value classes')
end
it 'rejects invalid values' do