Merge pull request #912 from matthiasbeyer/libimagruby/error-types

Libimagruby/error types
This commit is contained in:
Matthias Beyer 2017-03-08 12:45:16 +01:00 committed by GitHub
commit 3a0166ba7c
5 changed files with 105 additions and 26 deletions

View file

@ -79,11 +79,11 @@ macro_rules! call_on_fle_from_store {
$operation
},
Ok(None) => {
VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
VM::raise(Class::from_existing("RImagStoreReadError"), "Obj does not exist");
NilClass::new().to_any_object()
},
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreReadError"), e.description());
NilClass::new().to_any_object()
},
}
@ -100,11 +100,11 @@ macro_rules! call_on_fle_from_store {
$operation
},
Ok(None) => {
VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
VM::raise(Class::from_existing("RImagStoreReadError"), "Obj does not exist");
$ex
},
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreReadError"), e.description());
$ex
},
}
@ -133,7 +133,7 @@ methods!(
let entryheader = match typecheck!(hdr or return NilClass::new()).into_toml() {
Value::Table(t) => Value::Table(t),
_ => {
let ec = Class::from_existing("RuntimeError");
let ec = Class::from_existing("RImagEntryHeaderWriteError");
VM::raise(ec, "Something weird happened. Hash seems to be not a Hash");
return NilClass::new();
},
@ -160,7 +160,7 @@ methods!(
let content = match typecheck!(ctt).into_toml() {
Value::String(s) => s,
_ => {
let ec = Class::from_existing("RuntimeError");
let ec = Class::from_existing("RImagEntryError");
VM::raise(ec, "Something weird happened. String seems to be not a String");
return NilClass::new();
},
@ -197,7 +197,7 @@ methods!(
match itself.get_data(&*ENTRY_HEADER_WRAPPER).insert(&spec, obj.into_toml()) {
Ok(b) => Boolean::new(b),
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagEntryHeaderWriteError"), e.description());
Boolean::new(false)
}
}
@ -213,7 +213,7 @@ methods!(
Ok(Some(v)) => v.into_ruby(),
Ok(None) => NilClass::new().to_any_object(),
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagEntryHeaderWriteError"), e.description());
return Boolean::new(false).to_any_object();
}
}
@ -228,7 +228,7 @@ methods!(
Ok(Some(v)) => v.into_ruby(),
Ok(None) => NilClass::new().to_any_object(),
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagEntryHeaderReadError"), e.description());
return Boolean::new(false).to_any_object();
}
}

52
libimagruby/src/error.rs Normal file
View file

@ -0,0 +1,52 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use ruru::Class;
class!(RImagError);
class!(RImagObjDoesNotExistError);
class!(RImagStoreError);
class!(RImagStoreWriteError);
class!(RImagStoreReadError);
class!(RImagEntryError);
class!(RImagEntryHeaderError);
class!(RImagEntryHeaderReadError);
class!(RImagEntryHeaderWriteError);
class!(RImagTypeError);
pub fn setup() {
let imag_error = Class::new("RImagError", Some(&Class::from_existing("RuntimeError")));
Class::new("RImagObjDoesNotExistError" , Some(&imag_error));
{
let imag_store_error = Class::new("RImagStoreError", Some(&imag_error));
Class::new("RImagStoreWriteError", Some(&imag_store_error));
Class::new("RImagStoreReadError" , Some(&imag_store_error));
}
{
let imag_entry_error = Class::new("RImagEntryError" , Some(&imag_error));
let imag_entry_header_error = Class::new("RImagEntryHeaderError", Some(&imag_entry_error));
Class::new("RImagEntryHeaderReadError" , Some(&imag_entry_header_error));
Class::new("RImagEntryHeaderWriteError", Some(&imag_entry_header_error));
}
Class::new("RImagTypeError", Some(&imag_error));
}

View file

@ -34,6 +34,7 @@ extern crate libimagrt;
#[macro_use] pub mod store;
mod cache;
pub mod error;
pub mod entry;
pub mod imag;
pub mod ruby_utils;
@ -43,6 +44,7 @@ pub mod toml_utils;
#[no_mangle]
#[allow(non_snake_case)]
pub extern fn Init_liblibimagruby() {
self::error::setup();
self::store::setup();
self::storeid::setup();
self::entry::setup_filelockentry();

View file

@ -61,14 +61,14 @@ macro_rules! call_on_store_by_handle {
match hm.get($store_handle) {
Some($name) => { $operation },
None => {
VM::raise(Class::from_existing("RuntimeError"),
VM::raise(Class::from_existing("RImagStoreReadError"),
"Tried to operate on non-existing object");
$ex
}
}
},
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagError"), e.description());
$ex
}
}
@ -102,11 +102,11 @@ macro_rules! call_on_store {
let $fle_name = match $store_name.get($fle_handle_name) {
Ok(Some(fle)) => fle,
Ok(None) => {
VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
VM::raise(Class::from_existing("RImagStoreReadError"), "Obj does not exist");
return $fail_expr
},
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreReadError"), e.description());
return $fail_expr
},
};
@ -174,14 +174,14 @@ methods!(
let rtp = PathBuf::from(typecheck!(rtp or return any NilClass::new()).to_string());
if !rtp.exists() || !rtp.is_dir() {
VM::raise(Class::from_existing("RuntimeError"), "Runtimepath not a directory");
VM::raise(Class::from_existing("RImagError"), "Runtimepath not a directory");
return NilClass::new().to_any_object();
}
let store_config = match Configuration::new(&rtp) {
Ok(mut cfg) => cfg.store_config().cloned(),
Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagError"), e.description());
return NilClass::new().to_any_object();
} else {
warn!("No config file found.");
@ -248,7 +248,7 @@ methods!(
let store = match store {
Ok(s) => s,
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreError"), e.description());
return NilClass::new().to_any_object();
},
};
@ -264,7 +264,7 @@ methods!(
return store_handle.wrap().to_any_object();
},
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagError"), e.description());
return NilClass::new().to_any_object();
}
}
@ -291,7 +291,7 @@ methods!(
match store.create(sid.clone()) {
Err(e) => {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
NilClass::new().to_any_object()
},
Ok(entry) => {
@ -325,7 +325,7 @@ methods!(
match store.retrieve(sid.clone()) {
Err(e) => {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
NilClass::new().to_any_object()
},
Ok(entry) => {
@ -360,7 +360,7 @@ methods!(
match store.get(sid.clone()) {
Err(e) => {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
NilClass::new().to_any_object()
},
Ok(None) => NilClass::new().to_any_object(),
@ -397,7 +397,7 @@ methods!(
match store.retrieve_for_module(&name) {
Err(e) => {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
NilClass::new().to_any_object()
},
Ok(iter) => {
@ -428,7 +428,7 @@ methods!(
operation {
if let Err(e) = store.update(real_fle) {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
}
NilClass::new()
},
@ -451,7 +451,7 @@ methods!(
operation {
if let Err(e) = store.delete(sid) {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
}
NilClass::new()
},
@ -476,7 +476,7 @@ methods!(
operation {
if let Err(e) = store.save_to(&real_fle, sid) {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
}
NilClass::new()
},
@ -501,7 +501,7 @@ methods!(
operation {
if let Err(e) = store.save_as(real_fle, sid) {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
}
NilClass::new()
},
@ -525,7 +525,7 @@ methods!(
operation {
if let Err(e) = store.move_by_id(old, nw) {
trace_error(&e);
VM::raise(Class::from_existing("RuntimeError"), e.description());
VM::raise(Class::from_existing("RImagStoreWriteError"), e.description());
}
NilClass::new()
},

View file

@ -30,6 +30,31 @@ end
puts "---"
[
:RImag,
:RStoreId,
:RStoreHandle,
:RFileLockEntryHandle,
:REntryHeader,
:REntryContent,
:RImagError,
:RImagObjDoesNotExistError,
:RImagStoreError,
:RImagStoreWriteError,
:RImagStoreReadError,
:RImagEntryError,
:RImagEntryHeaderError,
:RImagEntryHeaderReadError,
:RImagEntryHeaderWriteError,
:RImagTypeError,
].each do |sym|
if Kernel.const_defined? sym
RImag.info "Exists: #{sym}"
else
RImag.error "#{sym} not defined"
end
end
works "RStoreId.new_baseless", (not RStoreId.new_baseless("test").nil?)
works "RStoreHandle.respond_to? :new", (RStoreHandle.respond_to? :new)