Skip to content

Commit

Permalink
Starlark error functions to return starlark::Error
Browse files Browse the repository at this point in the history
Summary: Updating some error functions in starlark to return `starlark::Error` instead of `anyhow::Error`. Helps with removing some `from_any`

Reviewed By: JakobDegen

Differential Revision: D67865727

fbshipit-source-id: 274347a40fc7799efffa21bb1e93b495dcb40dec
  • Loading branch information
Will-MingLun-Li authored and facebook-github-bot committed Jan 9, 2025
1 parent 247dcb9 commit 355510c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
5 changes: 2 additions & 3 deletions starlark/src/values/layout/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ where
}

/// Downcast.
pub fn new_err(value: Value<'v>) -> anyhow::Result<Self> {
pub fn new_err(value: Value<'v>) -> crate::Result<Self> {
match Self::new(value) {
Some(v) => Ok(v),
None => Err(value_error!(
"Expected value of type `{}`, got: `{}`",
T::TYPE,
value.to_string_for_type_error()
)
.into_anyhow()),
)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions starlark/src/values/layout/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'v, T: StarlarkValue<'v>> ValueTyped<'v, T> {

/// Downcast.
#[inline]
pub fn new_err(value: Value<'v>) -> anyhow::Result<ValueTyped<'v, T>> {
pub fn new_err(value: Value<'v>) -> crate::Result<ValueTyped<'v, T>> {
value.downcast_ref_err::<T>()?;
Ok(ValueTyped(value, marker::PhantomData))
}
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'v, T: StarlarkValue<'v>> FrozenValueTyped<'v, T> {

/// Downcast.
#[inline]
pub fn new_err(value: FrozenValue) -> anyhow::Result<FrozenValueTyped<'v, T>> {
pub fn new_err(value: FrozenValue) -> crate::Result<FrozenValueTyped<'v, T>> {
value.downcast_ref_err::<T>()?;
Ok(FrozenValueTyped(value, marker::PhantomData))
}
Expand Down
7 changes: 3 additions & 4 deletions starlark/src/values/layout/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,14 +1342,13 @@ pub trait ValueLike<'v>:

/// Get a reference to underlying data or [`Err`]
/// if contained object has different type than requested.
fn downcast_ref_err<T: StarlarkValue<'v>>(self) -> anyhow::Result<&'v T> {
fn downcast_ref_err<T: StarlarkValue<'v>>(self) -> crate::Result<&'v T> {
match self.downcast_ref() {
Some(v) => Ok(v),
None => Err(ValueValueError::WrongType(
None => Err(crate::Error::new_value(ValueValueError::WrongType(
T::TYPE,
self.to_value().to_string_for_type_error(),
)
.into()),
))),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion starlark/src/values/types/any_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
}

/// Obtain the value from a `Value`, if it is a `StarlarkAnyComplex<T>`.
pub fn get_err(value: Value<'v>) -> anyhow::Result<&'v T> {
pub fn get_err(value: Value<'v>) -> crate::Result<&'v T> {
value.downcast_ref_err::<Self>().map(|x| &x.value)
}
}
Expand Down

0 comments on commit 355510c

Please sign in to comment.