Skip to content

Commit

Permalink
Improves error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
xfbs committed May 2, 2023
1 parent 32382f9 commit ee711d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions languages/rust/oso/src/host/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ impl Instance {
.unwrap_or_else(|_| self.debug_type_name)
}

pub(crate) fn debug_name(&self) -> &'static str {
&self.debug_type_name

Check failure on line 383 in languages/rust/oso/src/host/class.rs

View workflow job for this annotation

GitHub Actions / check

this expression creates a reference which is immediately dereferenced by the compiler
}

/// Lookup an attribute on the instance via the registered `Class`
pub fn get_attr(&self, name: &str, host: &mut Host) -> crate::Result<PolarValue> {
tracing::trace!({ method = %name }, "get_attr");
Expand Down
6 changes: 5 additions & 1 deletion languages/rust/oso/src/host/from_polar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ where
if let PolarValue::Instance(instance) = val {
Ok(instance.downcast::<T>(None).map_err(|e| e.user())?.clone())
} else {
Err(TypeError::expected("Instance").user())
Err(
TypeError::expected(format!("Instance of {}", std::any::type_name::<T>()))
.got(val.type_name().to_string())
.user(),
)
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions languages/rust/oso/src/host/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,38 @@ This may mean you performed an operation in your policy over an unbound variable
};
Term::new_from_ffi(value)
}

pub fn type_name(&self) -> PolarValueType {
match self {
PolarValue::Integer(_) => PolarValueType::Integer,
PolarValue::Float(_) => PolarValueType::Float,
PolarValue::String(_) => PolarValueType::String,
PolarValue::Boolean(_) => PolarValueType::Boolean,
PolarValue::Map(_) => PolarValueType::Map,
PolarValue::List(_) => PolarValueType::List,
PolarValue::Variable(_) => PolarValueType::Variable,
PolarValue::Instance(i) => PolarValueType::Instance(i.debug_name()),
}
}
}

#[derive(Clone, Debug)]
pub enum PolarValueType {
Integer,
Float,
String,
Boolean,
Map,
List,
Variable,
Instance(&'static str),
}

impl std::fmt::Display for PolarValueType {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PolarValueType::Instance(name) => write!(fmt, "Instance<{}>", name),
_ => std::fmt::Debug::fmt(self, fmt),
}
}
}

0 comments on commit ee711d9

Please sign in to comment.