From 7d816f54a6df356e14bf0bee1d8a19fe1d2d3c5b Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Mon, 6 May 2024 19:27:08 +0200 Subject: [PATCH] feat(error): adding proper error messages to str:format when fmtlib fails to format --- src/arkreactor/Builtins/String.cpp | 16 +++++++++++++++- tests/errors/callable/fmt_arg_not_found.ark | 1 + tests/errors/callable/fmt_arg_not_found.expected | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/errors/callable/fmt_arg_not_found.ark create mode 100644 tests/errors/callable/fmt_arg_not_found.expected diff --git a/src/arkreactor/Builtins/String.cpp b/src/arkreactor/Builtins/String.cpp index d9b37f1cd..ec9dae7a2 100644 --- a/src/arkreactor/Builtins/String.cpp +++ b/src/arkreactor/Builtins/String.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -51,7 +52,20 @@ namespace Ark::internal::Builtins::String store.push_back(it->toString(*vm)); } - return Value(fmt::vformat(n[0].stringRef(), store)); + try + { + return Value(fmt::vformat(n[0].stringRef(), store)); + } + catch (fmt::format_error& e) + { + throw std::runtime_error( + fmt::format("str:format: can not format \"{}\" ({} argument{} provided) because of {}", + n[0].stringRef(), + n.size() - 1, + // if we have more than one argument (not counting the string to format), plural form + n.size() > 2 ? "s" : "", + e.what())); + } } /** diff --git a/tests/errors/callable/fmt_arg_not_found.ark b/tests/errors/callable/fmt_arg_not_found.ark new file mode 100644 index 000000000..6beb8dbf6 --- /dev/null +++ b/tests/errors/callable/fmt_arg_not_found.ark @@ -0,0 +1 @@ +(str:format "Hello {}, I'm {}" "World") diff --git a/tests/errors/callable/fmt_arg_not_found.expected b/tests/errors/callable/fmt_arg_not_found.expected new file mode 100644 index 000000000..e736837c1 --- /dev/null +++ b/tests/errors/callable/fmt_arg_not_found.expected @@ -0,0 +1 @@ +str:format: can not format "Hello {}, I'm {}" (1 argument provided) because of argument not found