1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/try_catch.h"
9 #include "gin/converter.h"
10 #include "v8/include/v8-message.h"
14 v8::Local<v8::String> GetSourceLine(v8::Isolate* isolate,
15 v8::Local<v8::Message> message) {
16 auto maybe = message->GetSourceLine(isolate->GetCurrentContext());
17 v8::Local<v8::String> source_line;
18 return maybe.ToLocal(&source_line) ? source_line : v8::String::Empty(isolate);
25 TryCatch::TryCatch(v8::Isolate* isolate)
26 : isolate_(isolate), try_catch_(isolate) {
29 TryCatch::~TryCatch() = default;
31 bool TryCatch::HasCaught() {
32 return try_catch_.HasCaught();
35 std::string TryCatch::GetStackTrace() {
41 v8::Local<v8::Message> message = try_catch_.Message();
42 ss << V8ToString(isolate_, message->Get()) << std::endl
43 << V8ToString(isolate_, GetSourceLine(isolate_, message)) << std::endl;
45 v8::Local<v8::StackTrace> trace = message->GetStackTrace();
49 int len = trace->GetFrameCount();
50 for (int i = 0; i < len; ++i) {
51 v8::Local<v8::StackFrame> frame = trace->GetFrame(isolate_, i);
52 ss << V8ToString(isolate_, frame->GetScriptName()) << ":"
53 << frame->GetLineNumber() << ":" << frame->GetColumn() << ": "
54 << V8ToString(isolate_, frame->GetFunctionName()) << std::endl;