try_catch.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.
  4. #include "gin/try_catch.h"
  5. #include <sstream>
  6. #include "gin/converter.h"
  7. #include "v8/include/v8-message.h"
  8. namespace {
  9. v8::Local<v8::String> GetSourceLine(v8::Isolate* isolate,
  10. v8::Local<v8::Message> message) {
  11. auto maybe = message->GetSourceLine(isolate->GetCurrentContext());
  12. v8::Local<v8::String> source_line;
  13. return maybe.ToLocal(&source_line) ? source_line : v8::String::Empty(isolate);
  14. }
  15. } // namespace
  16. namespace gin {
  17. TryCatch::TryCatch(v8::Isolate* isolate)
  18. : isolate_(isolate), try_catch_(isolate) {
  19. }
  20. TryCatch::~TryCatch() = default;
  21. bool TryCatch::HasCaught() {
  22. return try_catch_.HasCaught();
  23. }
  24. std::string TryCatch::GetStackTrace() {
  25. if (!HasCaught()) {
  26. return "";
  27. }
  28. std::stringstream ss;
  29. v8::Local<v8::Message> message = try_catch_.Message();
  30. ss << V8ToString(isolate_, message->Get()) << std::endl
  31. << V8ToString(isolate_, GetSourceLine(isolate_, message)) << std::endl;
  32. v8::Local<v8::StackTrace> trace = message->GetStackTrace();
  33. if (trace.IsEmpty())
  34. return ss.str();
  35. int len = trace->GetFrameCount();
  36. for (int i = 0; i < len; ++i) {
  37. v8::Local<v8::StackFrame> frame = trace->GetFrame(isolate_, i);
  38. ss << V8ToString(isolate_, frame->GetScriptName()) << ":"
  39. << frame->GetLineNumber() << ":" << frame->GetColumn() << ": "
  40. << V8ToString(isolate_, frame->GetFunctionName()) << std::endl;
  41. }
  42. return ss.str();
  43. }
  44. } // namespace gin