Skip to content

Commit

Permalink
feat(error): adding proper error messages to str:format when fmtlib f…
Browse files Browse the repository at this point in the history
…ails to format
  • Loading branch information
SuperFola committed May 6, 2024
1 parent ce12a4c commit 7d816f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/arkreactor/Builtins/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <utf8.hpp>
#include <fmt/args.h>
#include <fmt/core.h>
#include <fmt/format.h>

#include <Ark/TypeChecker.hpp>
#include <Ark/VM/VM.hpp>
Expand Down Expand Up @@ -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()));
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/errors/callable/fmt_arg_not_found.ark
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(str:format "Hello {}, I'm {}" "World")
1 change: 1 addition & 0 deletions tests/errors/callable/fmt_arg_not_found.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
str:format: can not format "Hello {}, I'm {}" (1 argument provided) because of argument not found

0 comments on commit 7d816f5

Please sign in to comment.