From 26520c5cf4b6a60da2c5cba971393f94b82f5939 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 2 Feb 2016 10:10:15 +0800 Subject: [PATCH] Improve error message for type error Close atom/electron#4307. --- native_mate/arguments.cc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/native_mate/arguments.cc b/native_mate/arguments.cc index 57aa552..ad62e20 100644 --- a/native_mate/arguments.cc +++ b/native_mate/arguments.cc @@ -9,6 +9,23 @@ namespace mate { +namespace { + +std::string V8TypeAsString(v8::Local value) { + if (value.IsEmpty()) + return ""; + if (value->IsUndefined()) + return "undefined"; + if (value->IsNull()) + return "null"; + std::string result; + if (!ConvertFromV8(NULL, value, &result)) + return std::string(); + return result; +} + +} // namespace + Arguments::Arguments() : isolate_(NULL), info_(NULL), @@ -37,7 +54,8 @@ v8::Local Arguments::ThrowError() const { return ThrowTypeError("Insufficient number of arguments."); return ThrowTypeError(base::StringPrintf( - "Error processing argument %d.", next_ - 1)); + "Error processing argument at index %d, conversion failure from %s", + next_, V8TypeAsString((*info_)[next_]).c_str())); } v8::Local Arguments::ThrowError(const std::string& message) const {