Update Media Router route details style.
[chromium-blink-merge.git] / gin / try_catch.cc
blobdf27b9c1e4f3dc924078ebc65a0128c76c37412c
1 // 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.
5 #include "gin/try_catch.h"
7 #include <sstream>
9 #include "gin/converter.h"
11 namespace {
13 v8::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);
20 } // namespace
22 namespace gin {
24 TryCatch::TryCatch(v8::Isolate* isolate)
25 : isolate_(isolate), try_catch_(isolate) {
28 TryCatch::~TryCatch() {
31 bool TryCatch::HasCaught() {
32 return try_catch_.HasCaught();
35 std::string TryCatch::GetStackTrace() {
36 if (!HasCaught()) {
37 return "";
40 std::stringstream ss;
41 v8::Local<v8::Message> message = try_catch_.Message();
42 ss << V8ToString(message->Get()) << std::endl
43 << V8ToString(GetSourceLine(isolate_, message)) << std::endl;
45 v8::Local<v8::StackTrace> trace = message->GetStackTrace();
46 if (trace.IsEmpty())
47 return ss.str();
49 int len = trace->GetFrameCount();
50 for (int i = 0; i < len; ++i) {
51 v8::Local<v8::StackFrame> frame = trace->GetFrame(i);
52 ss << V8ToString(frame->GetScriptName()) << ":"
53 << frame->GetLineNumber() << ":"
54 << frame->GetColumn() << ": "
55 << V8ToString(frame->GetFunctionName())
56 << std::endl;
58 return ss.str();
61 } // namespace gin