rhai icon indicating copy to clipboard operation
rhai copied to clipboard

FR: "Function not found" error should spec desired arity, and perhaps available arities

Open barries opened this issue 1 month ago • 2 comments

Right now, it's hard to tell which overload is missing when calling with a dynamic number of args. Adding the requested arity, and perhaps the available arities, would make this error much more easily diagnosable.

Thanks!

barries avatar Nov 13 '25 17:11 barries

Agree. However, this will be a breaking change, so would need to wait for the next major version.

schungx avatar Nov 14 '25 01:11 schungx

For others who need this soon, here's my miette-based code (not committed to miette, better suggestions appreciated, same-same for Rust code suggestions). This prints a message like: Function not found: postprocess_AD_Protocol_Spec_md(_1), available overload arities 0, 2:

        let arity = args.len();

        let result = match self.engine.call_fn::<Map>(
            &mut Scope::new(),
            &self.ast,
            fn_name,
            args
        ) {
            Err(err) => return Err(
                if matches!(&*err, rhai::EvalAltResult::ErrorFunctionNotFound(_, _)) {
                    let placeholders = (1..=arity)
                        .map(|placeholder_id| format!("_{placeholder_id}"))
                        .collect::<Vec<_>>()
                        .join(", ");

                    let available_arities = self.fn_defs // self.fn_defs: Vec<ScriptFuncDefs>
                        .keys()
                        .map(|arity| format!("{arity}"))
                        .collect::<Vec<_>>()
                        .join(", ");

                    miette!("{err}({placeholders}), available overload arities {available_arities}")
                } else {
                    miette!(err.to_string())
                }.context(self.name.clone())
            ),
            Ok(result) => result
        };

The placeholder _1 isn't a valid Rhai identifier, however because it's an obvious/self-documenting typical way of doing placeholders (even in the C++ stdlib!) I did it here; feel free to adapt as you see fit.

barries avatar Nov 14 '25 12:11 barries