From 5789f69d2d74f92973d1b9b2215f0dae7e44394b Mon Sep 17 00:00:00 2001 From: ochafik Date: Sat, 9 Nov 2024 18:57:09 +0000 Subject: [PATCH] `minja`: don't explode upon referencing a field on an array (fixes Hermes tool use template) --- common/minja.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/common/minja.hpp b/common/minja.hpp index a6e0bfcd4..979e53fe0 100644 --- a/common/minja.hpp +++ b/common/minja.hpp @@ -228,6 +228,9 @@ public: } Value get(const Value& key) { if (array_) { + if (!key.is_number_integer()) { + return Value(); + } auto index = key.get(); return array_->at(index < 0 ? array_->size() + index : index); } else if (object_) { @@ -618,7 +621,7 @@ public: Value evaluate(const std::shared_ptr & context) const { try { return do_evaluate(context); - } catch (const std::runtime_error & e) { + } catch (const std::exception & e) { std::ostringstream out; out << e.what(); if (location.source) out << error_location_suffix(*location.source, location.pos); @@ -769,7 +772,7 @@ public: void render(std::ostringstream & out, const std::shared_ptr & context) const { try { do_render(out, context); - } catch (const std::runtime_error & e) { + } catch (const std::exception & e) { std::ostringstream err; err << e.what(); if (location_.source) err << error_location_suffix(*location_.source, location_.pos); @@ -2152,7 +2155,7 @@ private: } } return tokens; - } catch (const std::runtime_error & e) { + } catch (const std::exception & e) { throw std::runtime_error(e.what() + error_location_suffix(*template_str, std::distance(start, it))); } }