Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(failed): show function #586

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modules/condition/failed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl SyntaxModule<ParserMetadata> for Failed {
self.is_parsed = true;
return Ok(());
} else {
return error!(meta, tok, "Failed expression must be followed by a block or statement")
return error!(meta, tok, format!("The function '{}' requires a 'failed' block or statement to handle errors", meta.get_token_at(meta.get_index() - 4).unwrap().word))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good rule of thumb: suspect any code with magic numbers, like "4" here; there will probably be some edge case not covered by it. For example:

hwalters@Ghostwheel ~/git/amber-lang (210) 
$ cat untrusted.ab 
$ echo 'Hello world' $
hwalters@Ghostwheel ~/git/amber-lang (210) 
$ amber untrusted.ab 
thread 'main' panicked at src/modules/condition/failed.rs:64:171:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'll see if I can find a better way of doing this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I wasn't sure too.
Maybe a check if there are parenthesis because probably with built-in doesn't work too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, the error message "The function '{}' requires..." implies that you expect the $...$ block to be run inside a function; but as my example shows, this is not always the case. I think you need to reword the error message so it doesn't reference a function name at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is exactly what we did before. So, based on the above comment, I would question whether we need this PR at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module cannot move back to get the function name. The function invocation should provide this module with the name

}
}
}
Expand Down