Function Call to JSON::Stringify is returning empty handle

132 views
Skip to first unread message

Vinayaka Kamath

unread,
Feb 9, 2021, 7:20:56 AM2/9/21
to v8-users
Hello All,

A call to JSON::Stringify is returning empty handle. I ensured that the parameters are valid. What are the other possible reasons? I tried retrieving the "JSON" object from the context using context->Global(), that fails and returns empty handle.  

bool toJSON(v8::Isolate *isolate, const v8::Local<v8::Value> &object,
std::string &json) {

   json.clear();

    if (object.IsEmpty()) {    // Object is not empty
        return true;
    }

     v8::HandleScope handle_scope(isolate);
     auto context = isolate->GetCurrentContext();
      std::cout << "Context: " << context.IsEmpty() << "\n";
      std::cout << "converting to string \n";
      v8::Local<v8::Value> result;
      if (!v8::JSON::Stringify(context, object).ToLocal(&result))      // This call is failing
           return false;
       json = *v8::String::Utf8Value(isolate, result);
       std::cout << "done converting to string \n" << json;
       return true;
}

Any help would be very much appreciated, thanks!

Jakob Kummerow

unread,
Feb 9, 2021, 7:26:31 AM2/9/21
to v8-users
The general pattern is that returned MaybeHandles (from any function, not just JSON::Stringify) are empty when an exception has been thrown by the operation. You can use a v8::TryCatch to catch this exception and inspect it, it'll include a helpful error message.


--

Vinayaka Kamath

unread,
Feb 9, 2021, 11:43:08 PM2/9/21
to v8-users
Hello Jakob, thanks for the reply. I put the code in another TryCatch, but it has not been caught and there's no error. I am confused. On repeatedly running the same code more thing once, occasionally I see the required message.

Please find the updated exception handling code here:

std::string describeException(v8::Isolate *isolate, v8::TryCatch *try_catch) {

std::ostringstream os;
v8::HandleScope handle_scope(isolate);
v8::TryCatch trycatch(isolate);
v8::String::Utf8Value exception(isolate, try_catch->Exception());
const char *exception_string = ToCString(exception);
auto context = isolate->GetCurrentContext();
v8::Local<v8::Message> message = try_catch->Message();

os << "Exception: ";
os << exception_string;
os << " " << std::endl;

// Print exception location details
if (!message.IsEmpty()) {
// Print location
v8::String::Utf8Value filename(isolate,
message->GetScriptOrigin().ResourceName());
v8::Local<v8::Context> context(isolate->GetCurrentContext());
const char *filename_string = ToCString(filename);
int linenum = message->GetLineNumber(context).FromJust();
os << "Location: " << filename_string << ":" << linenum << " " << std::endl;

// Print source code
// Print line of source code.
os << "Code: " << std::endl;
v8::String::Utf8Value sourceline(
isolate, message->GetSourceLine(context).ToLocalChecked());
const char* sourceline_string = ToCString(sourceline);
os << sourceline_string << std::endl;

// Print stack trace

v8::Local<v8::Value> stack_trace_string;
if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) &&
stack_trace_string->IsString() &&
v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) {
v8::String::Utf8Value stack_trace(isolate, stack_trace_string);
const char* stack_trace_string = ToCString(stack_trace);
os << stack_trace_string << std::endl;
}
}
if (trycatch.HasCaught()) {
v8::String::Utf8Value internal_exception(isolate, trycatch.Exception());
const char *internal_exception_string = ToCString(exception);
os << "Internal Error while trying to prepare exception string:" << std::endl;
os << internal_exception_string << std::endl;
}
return os.str();
}

I see that the exception messages are randomly blank despite me checking for try_catch.HasCaught()
Reply all
Reply to author
Forward
0 new messages