From 930e4983fc05979616903f41238624133bb20078 Mon Sep 17 00:00:00 2001 From: Pavel Medvedev Date: Sat, 15 Sep 2018 21:05:53 +0200 Subject: [PATCH] add `invalid_argument` exception and throw it from `convert::from_v8()` --- v8pp/convert.hpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/v8pp/convert.hpp b/v8pp/convert.hpp index 387ba8a8..3a99a051 100644 --- a/v8pp/convert.hpp +++ b/v8pp/convert.hpp @@ -44,6 +44,11 @@ struct convert }; */ +struct invalid_argument : std::invalid_argument +{ + invalid_argument(v8::Isolate* isolate, v8::Local value, char const* expected_type); +}; + // converter specializations for string types template struct convert> @@ -63,7 +68,7 @@ struct convert> { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected String"); + throw invalid_argument(isolate, value, "String"); } if (sizeof(Char) == 1) @@ -124,7 +129,7 @@ struct convert { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Boolean"); + throw invalid_argument(isolate, value, "Boolean"); } return value->BooleanValue(isolate->GetCurrentContext()).FromJust(); } @@ -200,7 +205,7 @@ struct convert::value>::type> { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Number"); + throw invalid_argument(isolate, value, "Number"); } if (bits <= 32) @@ -283,7 +288,7 @@ struct convert::value>::typ { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Number"); + throw invalid_argument(isolate, value, "Number"); } return static_cast(value->NumberValue(isolate->GetCurrentContext()).FromJust()); @@ -311,7 +316,7 @@ struct convert> { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Array"); + throw invalid_argument(isolate, value, "Array"); } v8::HandleScope scope(isolate); @@ -362,7 +367,7 @@ struct convert> { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Array"); + throw invalid_argument(isolate, value, "Array"); } v8::HandleScope scope(isolate); @@ -408,7 +413,7 @@ struct convert> { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Object"); + throw invalid_argument(isolate, value, "Object"); } v8::HandleScope scope(isolate); @@ -535,7 +540,7 @@ struct convert::value>::type> { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Object"); + throw invalid_argument(isolate, value, "Object"); } if (T* object = convert::from_v8(isolate, value)) { @@ -594,7 +599,7 @@ struct convert { if (!is_valid(isolate, value)) { - throw std::invalid_argument("expected Object"); + throw invalid_argument(isolate, value, "Object"); } if (std::shared_ptr object = convert>::from_v8(isolate, value)) { @@ -711,6 +716,14 @@ v8::Local to_local(v8::Isolate* isolate, v8::PersistentBase const& handle) } } +inline invalid_argument::invalid_argument(v8::Isolate* isolate, v8::Local value, char const* expected_type) + : std::invalid_argument(std::string("expected ") + + expected_type + + ", typeof=" + + (value.IsEmpty() ? std::string("") : v8pp::from_v8(isolate, value->TypeOf(isolate)))) +{ +} + } // namespace v8pp #endif // V8PP_CONVERT_HPP_INCLUDED