Skip to content

Commit

Permalink
'file_loader' use starlark error instead of anyhow
Browse files Browse the repository at this point in the history
Summary: Needed for anyhow->buck2_error migration in `buck2/app`

Reviewed By: epilatow, JakobDegen

Differential Revision: D64018263

fbshipit-source-id: dbd8bf162d69a0eff761e08dbe5ea1cc283e4494
  • Loading branch information
Will-MingLun-Li authored and facebook-github-bot committed Nov 26, 2024
1 parent decc502 commit 88b515d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
12 changes: 7 additions & 5 deletions starlark/src/environment/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,17 @@ impl Module {
&'v self,
module: &FrozenModule,
symbol: &str,
) -> anyhow::Result<Value<'v>> {
) -> crate::Result<Value<'v>> {
if Self::default_visibility(symbol) != Visibility::Public {
return Err(EnvironmentError::CannotImportPrivateSymbol(symbol.to_owned()).into());
return Err(crate::Error::new_other(
EnvironmentError::CannotImportPrivateSymbol(symbol.to_owned()),
));
}
match module.get_any_visibility(symbol)? {
(v, Visibility::Public) => Ok(v.owned_value(self.frozen_heap())),
(_, Visibility::Private) => {
Err(EnvironmentError::ModuleSymbolIsNotExported(symbol.to_owned()).into())
}
(_, Visibility::Private) => Err(crate::Error::new_other(
EnvironmentError::ModuleSymbolIsNotExported(symbol.to_owned()),
)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion starlark/src/eval/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn add_span_to_expr_error(
/// Convert syntax error to spanned evaluation exception
#[inline(always)]
pub(crate) fn expr_throw<'v, T>(
r: anyhow::Result<T>,
r: crate::Result<T>,
span: FrameSpan,
eval: &Evaluator<'v, '_, '_>,
) -> Result<T, EvalException> {
Expand Down
14 changes: 7 additions & 7 deletions starlark/src/eval/runtime/file_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::environment::FrozenModule;
/// A trait for turning a `path` given by a `load()` statement into a [`FrozenModule`].
pub trait FileLoader {
/// Open the file given by the load statement `path`.
fn load(&self, path: &str) -> anyhow::Result<FrozenModule>;
fn load(&self, path: &str) -> crate::Result<FrozenModule>;
}

/// [`FileLoader`] that looks up modules by name from a [`HashMap`].
Expand All @@ -41,13 +41,13 @@ pub struct ReturnFileLoader<'a> {
}

impl<'a> FileLoader for ReturnFileLoader<'a> {
fn load(&self, path: &str) -> anyhow::Result<FrozenModule> {
fn load(&self, path: &str) -> crate::Result<FrozenModule> {
match self.modules.get(path) {
Some(v) => Ok((*v).dupe()),
None => Err(anyhow::anyhow!(
None => Err(crate::Error::new_other(anyhow::anyhow!(
"ReturnFileLoader does not know the module `{}`",
path
)),
))),
}
}
}
Expand All @@ -60,13 +60,13 @@ pub(crate) struct ReturnOwnedFileLoader {

#[cfg(test)]
impl FileLoader for ReturnOwnedFileLoader {
fn load(&self, path: &str) -> anyhow::Result<FrozenModule> {
fn load(&self, path: &str) -> crate::Result<FrozenModule> {
match self.modules.get(path) {
Some(v) => Ok(v.dupe()),
None => Err(anyhow::anyhow!(
None => Err(crate::Error::new_other(anyhow::anyhow!(
"ReturnOwnedFileLoader does not know the module `{}`",
path
)),
))),
}
}
}

0 comments on commit 88b515d

Please sign in to comment.