-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNativeScriptException.mm
More file actions
230 lines (184 loc) · 6.7 KB
/
NativeScriptException.mm
File metadata and controls
230 lines (184 loc) · 6.7 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "NativeScriptException.h"
#import <Foundation/Foundation.h>
#include <sstream>
#include "js_native_api.h"
#include "js_native_api_types.h"
#ifdef ENABLE_JS_RUNTIME
#include "jsr.h"
#endif
#include "native_api_util.h"
#include "runtime/Runtime.h"
namespace nativescript {
NativeScriptException::NativeScriptException(const std::string& message) {
this->message_ = message;
this->name_ = "NativeScriptException";
}
NativeScriptException::NativeScriptException(const std::string& message,
const std::string& stackTrace) {
this->message_ = message;
this->stackTrace_ = stackTrace;
this->name_ = "NativeScriptException";
}
NativeScriptException::NativeScriptException(napi_env env, const std::string& message,
const std::string& name) {
this->message_ = message;
this->stackTrace_ = "";
this->fullMessage_ = message;
this->name_ = name;
}
NativeScriptException::NativeScriptException(napi_env env, napi_value error,
const std::string& message, const std::string& name) {
this->message_ = GetErrorMessage(env, error, message);
this->stackTrace_ = GetErrorStackTrace(env, error);
this->fullMessage_ = GetFullMessage(env, error, this->message_);
this->name_ = name;
napi_set_named_property(env, error, "name", napi_util::to_js_string(env, name));
NSLog(@"NativeScriptException::NativeScriptException: %s", this->fullMessage_.c_str());
}
std::string NativeScriptException::Description() const {
if (this->fullMessage_.empty()) {
return this->message_;
} else {
return this->fullMessage_;
}
}
void NativeScriptException::OnUncaughtError(napi_env env, napi_value error) {
#ifdef ENABLE_JS_RUNTIME
NapiScope scope(env);
#endif
std::stringstream fullMessageStream;
if (napi_util::has_property(env, error, "fullMessage")) {
fullMessageStream << napi_util::get_cxx_string(
env, napi_util::get_property(env, error, "fullMessage"));
} else {
#ifdef TARGET_ENGINE_V8
fullMessageStream << GetErrorStackTrace(env, error);
#else
fullMessageStream << GetErrorMessage(env, error);
fullMessageStream << "\n at \n" << GetErrorStackTrace(env, error);
#endif
}
// TODO: discardUncaughtJsExceptions
napi_value global;
napi_get_global(env, &global);
napi_value handler;
napi_get_named_property(env, global, "__onUncaughtError", &handler);
if (napi_util::is_of_type(env, handler, napi_function)) {
napi_value args[] = {error};
napi_value result;
napi_status status = napi_call_function(env, global, handler, 1, args, &result);
if (status != napi_ok) {
napi_get_and_clear_last_exception(env, &result);
if (napi_util::is_of_type(env, result, napi_object)) {
fullMessageStream << "\n\nError handler threw an error too:\n";
#ifdef TARGET_ENGINE_V8
fullMessageStream << GetErrorStackTrace(env, result);
#else
fullMessageStream << GetErrorMessage(env, result);
fullMessageStream << "\n at \n" << GetErrorStackTrace(env, result);
#endif
} else {
fullMessageStream
<< "\n\nError handler threw an error too, but we couldn't get the error object";
}
}
}
std::string fullMessage = fullMessageStream.str();
NSLog(@"NativeScriptException::OnUncaughtError: %s", fullMessage.c_str());
NSException* objcException = [NSException exceptionWithName:@"NativeScriptException"
reason:@(fullMessage.c_str())
userInfo:@{@"sender" : @"onUncaughtError"}];
// dispatch_async(dispatch_get_main_queue(), ^(void) {
@throw objcException;
// });
}
void NativeScriptException::ReThrowToJS(napi_env env, napi_value* errorOut) {
napi_value errObj;
if (!fullMessage_.empty()) {
napi_create_error(env, napi_util::to_js_string(env, name_),
napi_util::to_js_string(env, fullMessage_), &errObj);
} else if (!message_.empty()) {
napi_create_error(env, napi_util::to_js_string(env, name_),
napi_util::to_js_string(env, message_), &errObj);
} else {
napi_create_error(env, napi_util::to_js_string(env, name_),
napi_util::to_js_string(env, "No javascript exception or message provided."),
&errObj);
}
if (errorOut != nullptr) {
*errorOut = errObj;
} else {
napi_throw(env, errObj);
}
}
void NativeScriptException::PrintErrorMessage(const std::string& errorMessage) {
std::stringstream ss(errorMessage);
std::string line;
while (std::getline(ss, line, '\n')) {
NSLog(@"%s", line.c_str());
}
}
std::string NativeScriptException::GetErrorMessage(napi_env env, napi_value error,
const std::string& prependMessage) {
bool isError;
napi_is_error(env, error, &isError);
if (!isError) {
napi_value err;
napi_coerce_to_string(env, error, &err);
return napi_util::get_string_value(env, err);
}
napi_value message;
napi_get_named_property(env, error, "message", &message);
std::string mes = napi_util::get_string_value(env, message);
std::stringstream ss;
if (!prependMessage.empty()) {
ss << prependMessage << std::endl;
}
std::string errMessage;
bool hasFullErrorMessage = false;
napi_value fullMessage;
napi_get_named_property(env, error, "fullMessage", &fullMessage);
if (napi_util::is_of_type(env, fullMessage, napi_string)) {
hasFullErrorMessage = true;
errMessage = napi_util::get_string_value(env, fullMessage);
ss << errMessage;
}
if (!mes.empty()) {
if (hasFullErrorMessage) {
ss << std::endl;
}
ss << mes;
}
return ss.str();
}
std::string NativeScriptException::GetErrorStackTrace(napi_env env, napi_value error) {
std::stringstream ss;
bool isError;
napi_is_error(env, error, &isError);
if (!isError) return "";
napi_value stack;
napi_get_named_property(env, error, "stack", &stack);
std::string stackStr = napi_util::get_string_value(env, stack);
ss << stackStr;
return ss.str();
}
std::string NativeScriptException::GetFullMessage(napi_env env, napi_value error,
const std::string& jsExceptionMessage) {
bool isError;
napi_is_error(env, error, &isError);
if (!isError) {
return jsExceptionMessage;
}
std::string stackTraceMessage = GetErrorStackTrace(env, error);
std::stringstream ss;
#ifdef TARGET_ENGINE_V8
ss << stackTraceMessage;
#else
ss << jsExceptionMessage;
ss << std::endl << "StackTrace: " << std::endl << stackTraceMessage;
#endif
std::string loggedMessage = ss.str();
// PrintErrorMessage(loggedMessage);
return loggedMessage;
}
} // namespace nativescript