From 942d213191918ed0bc7aa5a6b62a1f0bc8d96bbb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Dec 2010 11:03:49 -0800 Subject: [PATCH] Raise a unique exception if libvirt C library not available. --- CHANGELOG.md | 3 ++- Gemfile.lock | 2 +- lib/ffi/libvirt.rb | 13 +++++++++++-- lib/ffi/libvirt/exception.rb | 10 ++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lib/ffi/libvirt/exception.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 5117e51..ab4f934 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.2.1 (unreleased) - + - `FFI::Libvirt::MissingLibError` is raised if libvirt C library + is not available. ## 0.2.0 (December 7, 2010) diff --git a/Gemfile.lock b/Gemfile.lock index f7fe370..41ba4a9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - libvirt (0.2.0) + libvirt (0.2.1.dev) ffi (~> 0.6.3) nokogiri (~> 1.4.3) diff --git a/lib/ffi/libvirt.rb b/lib/ffi/libvirt.rb index 25848f2..16f1fa3 100644 --- a/lib/ffi/libvirt.rb +++ b/lib/ffi/libvirt.rb @@ -7,10 +7,19 @@ module FFI # that it is up to you to manage all the pointers and so on that come # with this power. module Libvirt + autoload :MissingLibError, 'ffi/libvirt/exception' + autoload :Util, 'ffi/libvirt/util' + extend FFI::Library - ffi_lib "libvirt" - autoload :Util, 'ffi/libvirt/util' + # Attempt to load the libvirt lib and raise a more specific exception + # if it doesn't exist. (Normally a LoadError, which is ambiguous, is + # raised) + begin + ffi_lib "libvirt" + rescue LoadError + raise MissingLibError + end end end diff --git a/lib/ffi/libvirt/exception.rb b/lib/ffi/libvirt/exception.rb new file mode 100644 index 0000000..a57b7b4 --- /dev/null +++ b/lib/ffi/libvirt/exception.rb @@ -0,0 +1,10 @@ +module FFI + module Libvirt + # Error raised when the libvirt C library is missing. + class MissingLibError < StandardError + def initialize + super("The libvirt C library could not be loaded. Is it properly installed?") + end + end + end +end