blob: df27b9c1e4f3dc924078ebc65a0128c76c37412c [file] [log] [blame]
[email protected]855ab432013-11-18 17:09:361// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "gin/try_catch.h"
6
[email protected]2f703422013-11-25 21:26:157#include <sstream>
8
[email protected]855ab432013-11-18 17:09:369#include "gin/converter.h"
10
bashidbd2ef9bb2015-06-02 01:39:3211namespace {
12
13v8::Local<v8::String> GetSourceLine(v8::Isolate* isolate,
14 v8::Local<v8::Message> message) {
15 auto maybe = message->GetSourceLine(isolate->GetCurrentContext());
16 v8::Local<v8::String> source_line;
17 return maybe.ToLocal(&source_line) ? source_line : v8::String::Empty(isolate);
18}
19
20} // namespace
21
[email protected]855ab432013-11-18 17:09:3622namespace gin {
23
bashidbd2ef9bb2015-06-02 01:39:3224TryCatch::TryCatch(v8::Isolate* isolate)
25 : isolate_(isolate), try_catch_(isolate) {
[email protected]855ab432013-11-18 17:09:3626}
27
28TryCatch::~TryCatch() {
29}
30
31bool TryCatch::HasCaught() {
32 return try_catch_.HasCaught();
33}
34
[email protected]2f703422013-11-25 21:26:1535std::string TryCatch::GetStackTrace() {
[email protected]bf3dd3c2013-12-06 06:55:2536 if (!HasCaught()) {
37 return "";
38 }
39
[email protected]2f703422013-11-25 21:26:1540 std::stringstream ss;
deepak.sfaaa1b62015-04-30 07:30:4841 v8::Local<v8::Message> message = try_catch_.Message();
[email protected]2f703422013-11-25 21:26:1542 ss << V8ToString(message->Get()) << std::endl
bashidbd2ef9bb2015-06-02 01:39:3243 << V8ToString(GetSourceLine(isolate_, message)) << std::endl;
[email protected]855ab432013-11-18 17:09:3644
deepak.sfaaa1b62015-04-30 07:30:4845 v8::Local<v8::StackTrace> trace = message->GetStackTrace();
[email protected]2f703422013-11-25 21:26:1546 if (trace.IsEmpty())
47 return ss.str();
[email protected]855ab432013-11-18 17:09:3648
[email protected]2f703422013-11-25 21:26:1549 int len = trace->GetFrameCount();
50 for (int i = 0; i < len; ++i) {
deepak.sfaaa1b62015-04-30 07:30:4851 v8::Local<v8::StackFrame> frame = trace->GetFrame(i);
[email protected]2f703422013-11-25 21:26:1552 ss << V8ToString(frame->GetScriptName()) << ":"
53 << frame->GetLineNumber() << ":"
54 << frame->GetColumn() << ": "
55 << V8ToString(frame->GetFunctionName())
56 << std::endl;
57 }
58 return ss.str();
[email protected]855ab432013-11-18 17:09:3659}
60
61} // namespace gin