Repository navigation
Expand file tree
/
Copy pathJsError.cpp
More file actions
73 lines (63 loc) · 2.14 KB
/
Copy pathJsError.cpp
File metadata and controls
73 lines (63 loc) · 2.14 KB
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
#include "ns/JsError.h"
#include <utility>
#include "ns/V8Helpers.h"
namespace nativescript::common {
JsError::JsError(std::string message, JsErrorType type)
: message_(std::move(message)), type_(type) {}
JsError JsError::FromValue(v8::Isolate* isolate, v8::Local<v8::Value> value,
std::string message) {
JsError error(std::move(message));
if (!value.IsEmpty()) {
error.value_ = std::make_shared<v8::Global<v8::Value>>(isolate, value);
}
return error;
}
void JsError::ThrowCaught(v8::Isolate* isolate, const v8::TryCatch& tc,
const std::string& context) {
if (tc.HasTerminated() || isolate->IsExecutionTerminating()) {
throw Terminated();
}
v8::Local<v8::Value> exception = tc.Exception();
std::string message = context;
if (!exception.IsEmpty()) {
const std::string detail = ToString(isolate, exception);
if (!detail.empty()) {
message = message.empty() ? detail : message + ": " + detail;
}
}
throw FromValue(isolate, exception, std::move(message));
}
v8::Local<v8::Value> JsError::ToV8(v8::Isolate* isolate) const {
if (isolate->IsExecutionTerminating()) {
return v8::Local<v8::Value>();
}
if (value_ != nullptr && !value_->IsEmpty()) {
return value_->Get(isolate);
}
v8::Local<v8::String> text = ToV8String(isolate, message_);
switch (type_) {
case JsErrorType::kTypeError:
return v8::Exception::TypeError(text);
case JsErrorType::kRangeError:
return v8::Exception::RangeError(text);
case JsErrorType::kReferenceError:
return v8::Exception::ReferenceError(text);
case JsErrorType::kSyntaxError:
return v8::Exception::SyntaxError(text);
case JsErrorType::kError:
break;
}
return v8::Exception::Error(text);
}
void Terminated::ThrowToV8(v8::Isolate* isolate) const {
if (!isolate->IsExecutionTerminating()) {
isolate->ThrowException(v8::Exception::Error(ToV8String(isolate, what())));
}
}
void JsError::ThrowToV8(v8::Isolate* isolate) const {
v8::Local<v8::Value> value = ToV8(isolate);
if (!value.IsEmpty()) {
isolate->ThrowException(value);
}
}
} // namespace nativescript::common