summaryrefslogtreecommitdiff
path: root/libdeno/exceptions.cc
blob: 0d7bbed8be553c3d728838407a44674069df7345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#include "exceptions.h"
#include <string>

namespace deno {

std::string EncodeMessageAsJSON(v8::Local<v8::Context> context,
                                v8::Local<v8::Message> message) {
  auto* isolate = context->GetIsolate();
  v8::HandleScope handle_scope(isolate);
  v8::Context::Scope context_scope(context);

  auto stack_trace = message->GetStackTrace();

  // Encode the exception into a JS object, which we will then turn into JSON.
  auto json_obj = v8::Object::New(isolate);
  auto exception_str = message->Get();
  CHECK(json_obj->Set(context, v8_str("message"), exception_str).FromJust());

  auto maybe_source_line = message->GetSourceLine(context);
  if (!maybe_source_line.IsEmpty()) {
    CHECK(json_obj
              ->Set(context, v8_str("sourceLine"),
                    maybe_source_line.ToLocalChecked())
              .FromJust());
  }

  CHECK(json_obj
            ->Set(context, v8_str("scriptResourceName"),
                  message->GetScriptResourceName())
            .FromJust());

  auto maybe_line_number = message->GetLineNumber(context);
  if (maybe_line_number.IsJust()) {
    CHECK(json_obj
              ->Set(context, v8_str("lineNumber"),
                    v8::Integer::New(isolate, maybe_line_number.FromJust()))
              .FromJust());
  }

  CHECK(json_obj
            ->Set(context, v8_str("startPosition"),
                  v8::Integer::New(isolate, message->GetStartPosition()))
            .FromJust());

  CHECK(json_obj
            ->Set(context, v8_str("endPosition"),
                  v8::Integer::New(isolate, message->GetEndPosition()))
            .FromJust());

  CHECK(json_obj
            ->Set(context, v8_str("errorLevel"),
                  v8::Integer::New(isolate, message->ErrorLevel()))
            .FromJust());

  auto maybe_start_column = message->GetStartColumn(context);
  if (maybe_start_column.IsJust()) {
    auto start_column =
        v8::Integer::New(isolate, maybe_start_column.FromJust());
    CHECK(
        json_obj->Set(context, v8_str("startColumn"), start_column).FromJust());
  }

  auto maybe_end_column = message->GetEndColumn(context);
  if (maybe_end_column.IsJust()) {
    auto end_column = v8::Integer::New(isolate, maybe_end_column.FromJust());
    CHECK(json_obj->Set(context, v8_str("endColumn"), end_column).FromJust());
  }

  CHECK(json_obj
            ->Set(context, v8_str("isSharedCrossOrigin"),
                  v8::Boolean::New(isolate, message->IsSharedCrossOrigin()))
            .FromJust());

  CHECK(json_obj
            ->Set(context, v8_str("isOpaque"),
                  v8::Boolean::New(isolate, message->IsOpaque()))
            .FromJust());

  v8::Local<v8::Array> frames;
  if (!stack_trace.IsEmpty()) {
    uint32_t count = static_cast<uint32_t>(stack_trace->GetFrameCount());
    frames = v8::Array::New(isolate, count);

    for (uint32_t i = 0; i < count; ++i) {
      auto frame = stack_trace->GetFrame(isolate, i);
      auto frame_obj = v8::Object::New(isolate);
      CHECK(frames->Set(context, i, frame_obj).FromJust());
      auto line = v8::Integer::New(isolate, frame->GetLineNumber());
      auto column = v8::Integer::New(isolate, frame->GetColumn());
      CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust());
      CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust());
      CHECK(frame_obj
                ->Set(context, v8_str("functionName"), frame->GetFunctionName())
                .FromJust());
      // scriptName can be empty in special conditions e.g. eval
      auto scriptName = frame->GetScriptNameOrSourceURL();
      if (scriptName.IsEmpty()) {
        scriptName = v8_str("<unknown>");
      }
      CHECK(
          frame_obj->Set(context, v8_str("scriptName"), scriptName).FromJust());
      CHECK(frame_obj
                ->Set(context, v8_str("isEval"),
                      v8::Boolean::New(isolate, frame->IsEval()))
                .FromJust());
      CHECK(frame_obj
                ->Set(context, v8_str("isConstructor"),
                      v8::Boolean::New(isolate, frame->IsConstructor()))
                .FromJust());
      CHECK(frame_obj
                ->Set(context, v8_str("isWasm"),
                      v8::Boolean::New(isolate, frame->IsWasm()))
                .FromJust());
    }
  } else {
    // No stack trace. We only have one stack frame of info..
    frames = v8::Array::New(isolate, 1);

    auto frame_obj = v8::Object::New(isolate);
    CHECK(frames->Set(context, 0, frame_obj).FromJust());

    auto line =
        v8::Integer::New(isolate, message->GetLineNumber(context).FromJust());
    auto column =
        v8::Integer::New(isolate, message->GetStartColumn(context).FromJust());

    CHECK(frame_obj->Set(context, v8_str("line"), line).FromJust());
    CHECK(frame_obj->Set(context, v8_str("column"), column).FromJust());
    CHECK(frame_obj
              ->Set(context, v8_str("scriptName"),
                    message->GetScriptResourceName())
              .FromJust());
  }

  CHECK(json_obj->Set(context, v8_str("frames"), frames).FromJust());

  auto json_string = v8::JSON::Stringify(context, json_obj).ToLocalChecked();
  v8::String::Utf8Value json_string_(isolate, json_string);
  return std::string(ToCString(json_string_));
}

std::string EncodeExceptionAsJSON(v8::Local<v8::Context> context,
                                  v8::Local<v8::Value> exception) {
  auto* isolate = context->GetIsolate();
  v8::HandleScope handle_scope(isolate);
  v8::Context::Scope context_scope(context);

  auto message = v8::Exception::CreateMessage(isolate, exception);
  return EncodeMessageAsJSON(context, message);
}

void HandleException(v8::Local<v8::Context> context,
                     v8::Local<v8::Value> exception) {
  v8::Isolate* isolate = context->GetIsolate();
  DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
  std::string json_str = EncodeExceptionAsJSON(context, exception);
  CHECK_NOT_NULL(d);
  d->last_exception_ = json_str;
}

void HandleExceptionMessage(v8::Local<v8::Context> context,
                            v8::Local<v8::Message> message) {
  v8::Isolate* isolate = context->GetIsolate();
  DenoIsolate* d = DenoIsolate::FromIsolate(isolate);
  std::string json_str = EncodeMessageAsJSON(context, message);
  CHECK_NOT_NULL(d);
  d->last_exception_ = json_str;
}

}  // namespace deno