Skip to content

Commit 2f82505

Browse files
committed
fix: source map remapping to be consistent
1 parent 0e3728c commit 2f82505

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

NativeScript/runtime/Console.cpp

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,41 @@ std::vector<std::string> filterErrorLines(
7878
return result;
7979
}
8080

81+
std::string Console::RemapStackTrace(v8::Isolate* isolate, const std::string& stackTrace) {
82+
// Get the current context from the isolate
83+
Local<Context> context = isolate->GetCurrentContext();
84+
85+
// Get the global object
86+
Local<Object> global = context->Global();
87+
88+
// Get the __ns_remapStack function from global
89+
Local<Value> remapStackValue;
90+
bool success =
91+
global->Get(context, tns::ToV8String(isolate, "__ns_remapStack"))
92+
.ToLocal(&remapStackValue);
93+
94+
if (success && remapStackValue->IsFunction()) {
95+
Local<v8::Function> remapStackFunction =
96+
remapStackValue.As<v8::Function>();
97+
98+
// Prepare arguments - convert your string to V8 string
99+
Local<Value> args[] = {tns::ToV8String(isolate, stackTrace)};
100+
101+
// Call the function
102+
Local<Value> result;
103+
bool callSuccess =
104+
remapStackFunction->Call(context, global, 1, args).ToLocal(&result);
105+
106+
if (callSuccess && result->IsString()) {
107+
// If the function returns a modified string, use it
108+
return tns::ToString(isolate, result);
109+
}
110+
}
111+
112+
// Return original string if remapping failed or function not available
113+
return stackTrace;
114+
}
115+
81116
void Console::LogCallback(const FunctionCallbackInfo<Value>& args) {
82117
// TODO: implement 'forceLog' override option like android has, to force logs
83118
// in prod if desired
@@ -110,36 +145,8 @@ void Console::LogCallback(const FunctionCallbackInfo<Value>& args) {
110145
// Extract error details
111146
std::string errorTitle = "JavaScript Error";
112147

113-
// Get the current context from the isolate
114-
Local<Context> context = isolate->GetCurrentContext();
115-
116-
// Get the global object
117-
Local<Object> global = context->Global();
118-
119-
// Get the __ns_remapStack function from global
120-
Local<Value> remapStackValue;
121-
bool success =
122-
global->Get(context, tns::ToV8String(isolate, "__ns_remapStack"))
123-
.ToLocal(&remapStackValue);
124-
125-
if (success && remapStackValue->IsFunction()) {
126-
Local<v8::Function> remapStackFunction =
127-
remapStackValue.As<v8::Function>();
128-
129-
// Prepare arguments - convert your string to V8 string
130-
Local<Value> args[] = {tns::ToV8String(isolate, errorToDisplay)};
131-
132-
// Call the function
133-
Local<Value> result;
134-
bool callSuccess =
135-
remapStackFunction->Call(context, global, 1, args).ToLocal(&result);
136-
137-
if (callSuccess && result->IsString()) {
138-
// If the function returns a modified string, use it
139-
std::string remappedError = tns::ToString(isolate, result);
140-
errorToDisplay = remappedError; // Update the error to display
141-
}
142-
}
148+
// Apply source map remapping to the error display
149+
errorToDisplay = RemapStackTrace(isolate, errorToDisplay);
143150

144151
try {
145152
NativeScriptException::ShowErrorModal(errorTitle, errorToDisplay,
@@ -158,7 +165,15 @@ void Console::LogCallback(const FunctionCallbackInfo<Value>& args) {
158165
verbosityLevelUpper.begin(), ::toupper);
159166

160167
std::stringstream ss;
161-
ss << stringResult;
168+
std::string processedStringResult = stringResult;
169+
170+
// Apply source map remapping if this contains a stack trace
171+
bool hasStackTrace = isStackFrame(stringResult);
172+
if (hasStackTrace) {
173+
processedStringResult = RemapStackTrace(isolate, processedStringResult);
174+
}
175+
176+
ss << processedStringResult;
162177

163178
if (verbosityLevel == "trace") {
164179
std::string stacktrace = tns::GetStackTrace(isolate);

NativeScript/runtime/Console.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Console {
2525
static const v8::Local<v8::String> BuildStringFromArg(v8::Local<v8::Context> context, const v8::Local<v8::Value>& val);
2626
static const v8::Local<v8::String> TransformJSObject(v8::Local<v8::Object> object);
2727
static ConsoleAPIType VerbosityToInspectorMethod(const std::string level);
28+
static std::string RemapStackTrace(v8::Isolate* isolate, const std::string& stackTrace);
2829

2930
static void SendToDevToolsFrontEnd(ConsoleAPIType method,
3031
const v8::FunctionCallbackInfo<v8::Value>& args);

NativeScript/runtime/NativeScriptException.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,12 @@
296296
}
297297
}
298298

299-
// For non-critical exceptions, just re-throw normally
299+
// For non-critical exceptions:
300300
if (RuntimeConfig.IsDebug) {
301-
Log(@"Debug mode - converting V8 exception to safe log instead of throw");
302-
Log(@"Would have thrown: %s", this->message_.c_str());
301+
// Be gentle, state case in logs and allow developer to continue
302+
Log(@"Debug mode - suppressing throw to continue: %s", this->message_.c_str());
303303
} else {
304+
// just re-throw normally
304305
isolate->ThrowException(errObj);
305306
}
306307
} @catch (NSException* exception) {

0 commit comments

Comments
 (0)