use crate::{binding::vm, util, AnyObject, Array, Class, Object, RString};
pub trait Exception: Object {
fn new(class: &str, msg: Option<&str>) -> Self {
let class = util::inmost_rb_object(class);
let msg = msg.map(|s| RString::new_utf8(s).value());
Self::from(vm::call_method(class, "new", util::option_to_slice(&msg)))
}
fn exception(&self, string: Option<&str>) -> Self {
let string = string.map(|s| RString::new_utf8(s).value());
Self::from(vm::call_method(
self.value(),
"exception",
util::option_to_slice(&string),
))
}
fn backtrace(&self) -> Option<Array> {
let result = vm::call_method(self.value(), "backtrace", &[]);
if result.is_nil() {
return None;
}
Some(Array::from(result))
}
fn backtrace_locations(&self) -> Option<Array> {
let result = vm::call_method(self.value(), "backtrace_locations", &[]);
if result.is_nil() {
return None;
}
Some(Array::from(result))
}
fn cause(&self) -> Option<Self> {
let result = vm::call_method(self.value(), "cause", &[]);
if result.is_nil() {
return None;
}
Some(Self::from(result))
}
fn inspect(&self) -> String {
RString::from(vm::call_method(self.value(), "inspect", &[])).to_string()
}
fn message(&self) -> String {
RString::from(vm::call_method(self.value(), "message", &[])).to_string()
}
fn set_backtrace(&self, backtrace: AnyObject) -> Option<Array> {
let result = vm::call_method(self.value(), "set_backtrace", &[backtrace.value()]);
if result.is_nil() {
return None;
}
Some(Array::from(result))
}
fn to_s(&self) -> String {
RString::from(vm::call_method(self.value(), "to_s", &[])).to_string()
}
}