MCPcopy Index your code
hub / github.com/nodejs/node / getErrorSource

Function getErrorSource

lib/internal/source_map/prepare_stack_trace.js:149–176  ·  view source on GitHub ↗

* Return a snippet of code from where the exception was originally thrown * above the stack trace. This called from GetErrorSource in node_errors.cc. * @param {import('internal/source_map/source_map').SourceMap} sourceMap - the source map to be used * @param {string} originalSourcePath - path or

(
  sourceMap,
  originalSourcePath,
  originalLine,
  originalColumn,
)

Source from the content-addressed store, hash-verified

147 * @returns {string | undefined} - the exact line in the source content or undefined if file not found
148 */
149function getErrorSource(
150 sourceMap,
151 originalSourcePath,
152 originalLine,
153 originalColumn,
154) {
155 const line = getSourceLine(sourceMap, originalSourcePath, originalLine);
156 if (!line) {
157 return;
158 }
159 const originalSourcePathNoScheme =
160 StringPrototypeStartsWith(originalSourcePath, 'file://') ?
161 fileURLToPath(originalSourcePath) : originalSourcePath;
162
163 // Display ^ in appropriate position, regardless of whether tabs or
164 // spaces are used:
165 let prefix = '';
166 for (const character of new SafeStringIterator(
167 StringPrototypeSlice(line, 0, originalColumn + 1))) {
168 prefix += character === '\t' ? '\t' :
169 StringPrototypeRepeat(' ', getStringWidth(character));
170 }
171 prefix = StringPrototypeSlice(prefix, 0, -1); // The last character is '^'.
172
173 const exceptionLine =
174 `${originalSourcePathNoScheme}:${originalLine + 1}\n${line}\n${prefix}^\n`;
175 return exceptionLine;
176}
177
178/**
179 * Retrieve exact line in the original source code from the source map's `sources` list or disk.

Callers 1

getSourceMapErrorSourceFunction · 0.85

Calls 2

getSourceLineFunction · 0.85
fileURLToPathFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…