-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
Instead of an unfriendly LocalJumpError, raise the Emoji::NotFound exception with a helpful message if the block was not given. If a fallback block was given, yield the value for which the lookup failed.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
module Emoji | ||
extend self | ||
|
||
NotFound = Class.new(IndexError) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mislav
Author
Contributor
|
||
|
||
def data_file | ||
File.expand_path('../../db/emoji.json', __FILE__) | ||
end | ||
|
@@ -17,11 +19,19 @@ def all | |
end | ||
|
||
def find_by_alias(name) | ||
names_index.fetch(name) { yield } | ||
names_index.fetch(name) { | ||
if block_given? then yield name | ||
else raise NotFound, "Emoji not found by name: %s" % name.inspect | ||
end | ||
} | ||
end | ||
This comment has been minimized.
Sorry, something went wrong.
jeremy
Contributor
|
||
|
||
def find_by_unicode(unicode) | ||
unicodes_index.fetch(unicode) { yield } | ||
unicodes_index.fetch(unicode) { | ||
if block_given? then yield unicode | ||
else raise NotFound, "Emoji not found from unicode: %s" % Emoji::Character.hex_inspect(unicode) | ||
end | ||
} | ||
end | ||
|
||
def names | ||
|
3 comments
on commit 5d24012
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this API style pretty surprising, since I expect a missing lookup to return nil
for nice operation with truthy conditionals, like
if emoji = Emoji.find_by_alias($1)
...
else
...
end
Raising an exception when the alias is missing does not feel actually exceptional—it's an expected common case. By raising, developers will often end up rescuing the exception for control flow rather than looking deeper to see that find_by_*
has Hash#fetch
-like semantics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeremy yeah, I agree, this is kinda a smell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted a nil-free API. Agreed that it makes the truthy conditional a little more awkward by forcing you to append a { nil }
block. I don't expect developers to start using these exceptions as control flow, because if they do, that's their own fault. I think in some cases an exception is in order if the user think they're sure that an emoji by that name exists, but for some reason it doesn't (or gets removed in the future).
Suggestions for another API are welcome, but we'd have to do another major or minor release with this. v2.0 is already out.
IndexError
is used for out of bounds indexes on Arrays. Probably want to subclassKeyError
for a map lookup, or just useStandardError
.