GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — process: split routines used to enhance fatal exception stack traces · acohn/node@9445492

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • events.js
    • internal
      • bootstrap
        • node.js
      • errors.js
      • process
        • execution.js
      • util.js
      • util
        • inspect.js
  • src
    • env.h
    • inspector_agent.cc
    • inspector_agent.h
    • node_errors.cc
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ const { Math, Object, Reflect } = primordials;
2525

2626
var spliceOne;
2727

28+
const {
29+
kEnhanceStackBeforeInspector,
30+
codes
31+
} = require('internal/errors');
2832
const {
2933
ERR_INVALID_ARG_TYPE,
3034
ERR_OUT_OF_RANGE,
3135
ERR_UNHANDLED_ERROR
32-
} = require('internal/errors').codes;
36+
} = codes;
3337

3438
const {
3539
inspect
@@ -142,8 +146,8 @@ function enhanceStackTrace(err, own) {
142146
ownStack.splice(off + 1, len - 2,
143147
' [... lines matching original stack trace ...]');
144148
}
145-
// Do this last, because it is the only operation with side effects.
146-
err.stack = err.stack + sep + ownStack.join('\n');
149+
150+
return err.stack + sep + ownStack.join('\n');
147151
}
148152

149153
EventEmitter.prototype.emit = function emit(type, ...args) {
@@ -162,11 +166,10 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
162166
er = args[0];
163167
if (er instanceof Error) {
164168
try {
165-
const { kExpandStackSymbol } = require('internal/util');
166169
const capture = {};
167170
// eslint-disable-next-line no-restricted-syntax
168171
Error.captureStackTrace(capture, EventEmitter.prototype.emit);
169-
Object.defineProperty(er, kExpandStackSymbol, {
172+
Object.defineProperty(er, kEnhanceStackBeforeInspector, {
170173
value: enhanceStackTrace.bind(null, er, capture),
171174
configurable: true
172175
});
Collapse file

‎lib/internal/bootstrap/node.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js
+15-2Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,22 @@ process.emitWarning = emitWarning;
299299
}
300300

301301
function setupPrepareStackTrace() {
302-
const { setPrepareStackTraceCallback } = internalBinding('errors');
303-
const { prepareStackTrace } = require('internal/errors');
302+
const {
303+
setEnhanceStackForFatalException,
304+
setPrepareStackTraceCallback
305+
} = internalBinding('errors');
306+
const {
307+
prepareStackTrace,
308+
fatalExceptionStackEnhancers: {
309+
beforeInspector,
310+
afterInspector
311+
}
312+
} = require('internal/errors');
313+
// Tell our PrepareStackTraceCallback passed to the V8 API
314+
// to call prepareStackTrace().
304315
setPrepareStackTraceCallback(prepareStackTrace);
316+
// Set the function used to enhance the error stack for printing
317+
setEnhanceStackForFatalException(beforeInspector, afterInspector);
305318
}
306319

307320
function setupProcessObject() {
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,45 @@ function addNumericalSeparator(val) {
622622
return `${val.slice(0, i)}${res}`;
623623
}
624624

625+
// Used to enhance the stack that will be picked up by the inspector
626+
const kEnhanceStackBeforeInspector = Symbol('kEnhanceStackBeforeInspector');
627+
628+
// These are supposed to be called only on fatal exceptions before
629+
// the process exits.
630+
const fatalExceptionStackEnhancers = {
631+
beforeInspector(error) {
632+
if (typeof error[kEnhanceStackBeforeInspector] !== 'function') {
633+
return error.stack;
634+
}
635+
636+
try {
637+
// Set the error.stack here so it gets picked up by the
638+
// inspector.
639+
error.stack = error[kEnhanceStackBeforeInspector]();
640+
} catch {
641+
// We are just enhancing the error. If it fails, ignore it.
642+
}
643+
return error.stack;
644+
},
645+
afterInspector(error) {
646+
const originalStack = error.stack;
647+
const {
648+
inspect,
649+
inspectDefaultOptions: {
650+
colors: defaultColors
651+
}
652+
} = lazyInternalUtilInspect();
653+
const colors = internalBinding('util').guessHandleType(2) === 'TTY' &&
654+
require('internal/tty').hasColors() ||
655+
defaultColors;
656+
try {
657+
return inspect(error, { colors });
658+
} catch {
659+
return originalStack;
660+
}
661+
}
662+
};
663+
625664
module.exports = {
626665
addCodeToName, // Exported for NghttpError
627666
codes,
@@ -638,6 +677,8 @@ module.exports = {
638677
// This is exported only to facilitate testing.
639678
E,
640679
prepareStackTrace,
680+
kEnhanceStackBeforeInspector,
681+
fatalExceptionStackEnhancers
641682
};
642683

643684
// To declare an error message, use the E(sym, val, def) function above. The sym
Collapse file

‎lib/internal/process/execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/execution.js
-12Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ function createOnGlobalUncaughtException() {
150150
} else if (!process.emit('uncaughtException', er, type)) {
151151
// If someone handled it, then great. Otherwise, die in C++ land
152152
// since that means that we'll exit the process, emit the 'exit' event.
153-
const { inspect } = require('internal/util/inspect');
154-
const colors = internalBinding('util').guessHandleType(2) === 'TTY' &&
155-
require('internal/tty').hasColors() ||
156-
inspect.defaultOptions.colors;
157153
try {
158154
if (!process._exiting) {
159155
process._exiting = true;
@@ -163,14 +159,6 @@ function createOnGlobalUncaughtException() {
163159
} catch {
164160
// Nothing to be done about it at this point.
165161
}
166-
try {
167-
const { kExpandStackSymbol } = require('internal/util');
168-
if (typeof er[kExpandStackSymbol] === 'function')
169-
er[kExpandStackSymbol]();
170-
er.stack = inspect(er, { colors });
171-
} catch {
172-
// Nothing to be done about it at this point.
173-
}
174162
return false;
175163
}
176164

Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,5 @@ module.exports = {
414414
// Used by the buffer module to capture an internal reference to the
415415
// default isEncoding implementation, just in case userland overrides it.
416416
kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
417-
kExpandStackSymbol: Symbol('kExpandStackSymbol'),
418417
kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol')
419418
};
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,5 +1683,6 @@ function formatWithOptions(inspectOptions, ...args) {
16831683
module.exports = {
16841684
inspect,
16851685
format,
1686-
formatWithOptions
1686+
formatWithOptions,
1687+
inspectDefaultOptions
16871688
};
Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ constexpr size_t kFsStatsBufferLength =
408408
V(crypto_key_object_constructor, v8::Function) \
409409
V(domain_callback, v8::Function) \
410410
V(domexception_function, v8::Function) \
411+
V(enhance_fatal_stack_after_inspector, v8::Function) \
412+
V(enhance_fatal_stack_before_inspector, v8::Function) \
411413
V(fs_use_promises_symbol, v8::Symbol) \
412414
V(host_import_module_dynamically_callback, v8::Function) \
413415
V(host_initialize_import_meta_object_callback, v8::Function) \
Collapse file

‎src/inspector_agent.cc‎

Copy file name to clipboardExpand all lines: src/inspector_agent.cc
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ class NodeInspectorClient : public V8InspectorClient {
570570
}
571571
}
572572

573-
void FatalException(Local<Value> error, Local<Message> message) {
573+
void ReportUncaughtException(Local<Value> error, Local<Message> message) {
574574
Isolate* isolate = env_->isolate();
575575
Local<Context> context = env_->context();
576576

@@ -836,10 +836,11 @@ void Agent::WaitForDisconnect() {
836836
}
837837
}
838838

839-
void Agent::FatalException(Local<Value> error, Local<Message> message) {
839+
void Agent::ReportUncaughtException(Local<Value> error,
840+
Local<Message> message) {
840841
if (!IsListening())
841842
return;
842-
client_->FatalException(error, message);
843+
client_->ReportUncaughtException(error, message);
843844
WaitForDisconnect();
844845
}
845846

Collapse file

‎src/inspector_agent.h‎

Copy file name to clipboardExpand all lines: src/inspector_agent.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class Agent {
6565
void WaitForConnect();
6666
// Blocks till all the sessions with "WaitForDisconnectOnShutdown" disconnect
6767
void WaitForDisconnect();
68-
void FatalException(v8::Local<v8::Value> error,
69-
v8::Local<v8::Message> message);
68+
void ReportUncaughtException(v8::Local<v8::Value> error,
69+
v8::Local<v8::Message> message);
7070

7171
// Async stack traces instrumentation.
7272
void AsyncTaskScheduled(const v8_inspector::StringView& taskName, void* task,

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.