Permalink
Browse files

Make an Error subclass for missing items

  • Loading branch information...
1 parent 3494443 commit 843d52d156b4d14c51743ff4b05ebb4d772e33cd @drbrain committed Mar 28, 2011
Showing with 13 additions and 6 deletions.
  1. +5 −3 ext/pasteboard/pasteboard.c
  2. +8 −3 lib/pasteboard.rb
@@ -8,6 +8,7 @@ static VALUE cPB;
static VALUE cPBType;
static VALUE cPBTypeEncodings;
static VALUE ePBError;
+static VALUE ePBMissing;
#if HAVE_RB_STR_ENCODE
#include <ruby/encoding.h>
@@ -109,7 +110,7 @@ pb_error(OSStatus err) {
rb_raise(ePBError,
"pasteboard has been modified and must be synchronized before use");
case badPasteboardIndexErr:
- rb_raise(ePBError, "pasteboard item does not exist");
+ rb_raise(ePBMissing, "item does not exist");
case badPasteboardItemErr:
rb_raise(ePBError, "item reference does not exist");
case badPasteboardFlavorErr:
@@ -436,8 +437,9 @@ void
Init_pasteboard(void) {
cPB = rb_define_class("Pasteboard", rb_cObject);
- cPBType = rb_const_get_at(cPB, rb_intern("Type"));
- ePBError = rb_const_get_at(cPB, rb_intern("Error"));
+ cPBType = rb_const_get_at(cPB, rb_intern("Type"));
+ ePBError = rb_const_get_at(cPB, rb_intern("Error"));
+ ePBMissing = rb_const_get_at(cPB, rb_intern("Missing"));
#if HAVE_RB_STR_ENCODE
cPBTypeEncodings = rb_const_get_at(cPBType, rb_intern("Encodings"));
View
@@ -31,6 +31,12 @@ class Error < RuntimeError
end
##
+ # Missing is raised when attempting to access an item that does not exist
+
+ class Missing < Error
+ end
+
+ ##
# Version of pasteboard
VERSION = '1.0'
@@ -91,9 +97,8 @@ def [] index, flavor = nil
id = get_item_identifier index + 1
get id, flavor
- rescue => e
- return nil if e.message.end_with? 'does not exist' # HACK
- raise
+ rescue Missing => e
+ return nil
end
##

0 comments on commit 843d52d

Please sign in to comment.