Skip to content

Commit

Permalink
errors: ErrorKind::Fail
Browse files Browse the repository at this point in the history
Summary: ^

Reviewed By: stepancheg

Differential Revision: D51735688

fbshipit-source-id: f0d1cfd18acfc2142869b75689d2c9b43259b5db
  • Loading branch information
JakobDegen authored and facebook-github-bot committed Dec 1, 2023
1 parent cb74c94 commit 7d4e885
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions starlark/src/stdlib/funcs/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub(crate) fn register_other(builder: &mut GlobalsBuilder) {
/// fail("oops", 1, False) # fail: oops 1 False
/// # "#, "oops 1 False");
/// ```
fn fail(#[starlark(args)] args: UnpackTuple<Value>) -> anyhow::Result<StarlarkNever> {
fn fail(#[starlark(args)] args: UnpackTuple<Value>) -> starlark::Result<StarlarkNever> {
let mut s = String::new();
for x in args.items {
s.push(' ');
Expand All @@ -88,7 +88,9 @@ pub(crate) fn register_other(builder: &mut GlobalsBuilder) {
None => x.collect_repr(&mut s),
}
}
Err(anyhow::anyhow!("fail:{}", s))
Err(starlark::Error::new(starlark::ErrorKind::Fail(
anyhow::Error::msg(s),
)))
}

/// Take the absolute value of an int.
Expand Down
5 changes: 5 additions & 0 deletions starlark_syntax/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ impl fmt::Debug for Error {
/// The different kinds of errors that can be produced by starlark
#[non_exhaustive]
pub enum ErrorKind {
/// An explicit `fail` invocation
Fail(anyhow::Error),
/// An error approximately associated with a value.
///
/// Includes unsupported operations, missing attributes, things of that sort.
Expand All @@ -173,6 +175,7 @@ impl ErrorKind {
/// The source of the error, akin to `[std::error::Error::source]`
pub fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Fail(_) => None,
Self::Value(_) => None,
Self::Internal(_) => None,
Self::Other(e) => e.source(),
Expand All @@ -183,6 +186,7 @@ impl ErrorKind {
impl fmt::Debug for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Fail(s) => write!(f, "fail:{}", s),
Self::Value(e) => fmt::Debug::fmt(e, f),
Self::Internal(e) => write!(f, "Internal error: {}", e),
Self::Other(e) => fmt::Debug::fmt(e, f),
Expand All @@ -193,6 +197,7 @@ impl fmt::Debug for ErrorKind {
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Fail(s) => write!(f, "fail:{}", s),
Self::Value(e) => fmt::Display::fmt(e, f),
Self::Internal(e) => write!(f, "Internal error: {}", e),
Self::Other(e) => fmt::Display::fmt(e, f),
Expand Down

0 comments on commit 7d4e885

Please sign in to comment.