Resolve-service
Utilities for converting a parsed stack trace into formatted, optionally source-enriched stack entries.
The main entry point is resolveError(parsedStack, options), which:
- Formats each frame into a single stack line (
format) - Optionally enriches frames with highlighted source context (
code) whenoptions.getSource()can provide aSourceService - Filters native frames unless
options.withNativeFramesis enabled
Imports
ts
import { resolveError, Bias, SourceService } from '@remotex-labs/xmap';
// Stack parsing lives in the parser component:
import { parseErrorStack } from '@remotex-labs/xmap/parser.component';You can also import directly:
ts
import { resolveError } from '@remotex-labs/xmap/resolve.service';
import { SourceService, Bias } from '@remotex-labs/xmap';
import { parseErrorStack } from '@remotex-labs/xmap/parser.component';Quick Start
ts
import { resolveError, Bias, SourceService } from '@remotex-labs/xmap';
import { parseErrorStack } from '@remotex-labs/xmap/parser.component';
try {
throw new Error('Boom');
} catch (e) {
const parsed = parseErrorStack(e as Error);
const resolved = resolveError(parsed, {
bias: Bias.BOUND,
withNativeFrames: false
});
// Each entry has a pre-formatted stack line.
for (const entry of resolved.stack) {
console.log(entry.format);
if (entry.code) console.log(entry.code);
}
}Enriching Frames With Source Context
To get entry.code for a frame, resolveError() needs a SourceService for the frame's fileName via options.getSource.
ts
import { resolveError, Bias, SourceService } from '@remotex-labs/xmap';
import { parseErrorStack } from '@remotex-labs/xmap/parser.component';
// Example: build a SourceService from a source map payload for your generated bundle.
const bundleMapJson = '{ "version": 3, "file": "dist/bundle.js", "sources": ["src/app.ts"], "names": [], "mappings": "AAAA", "sourcesContent": ["console.log(1)\\n"] }';
const bundleSource = new SourceService(bundleMapJson);
function getSource(fileName: string): SourceService | undefined {
// Keep this mapping logic consistent with how your stack frames render file paths.
// For example, you might normalize `fileName` or match by suffix.
if (fileName.endsWith('dist/bundle.js')) return bundleSource;
return undefined;
}
try {
throw new Error('Boom');
} catch (e) {
const parsed = parseErrorStack(e as Error);
const resolved = resolveError(parsed, {
getSource,
bias: Bias.LOWER_BOUND,
linesBefore: 3,
linesAfter: 4
});
for (const entry of resolved.stack) {
console.log(entry.format);
if (entry.code) console.log(entry.code);
}
}Notes:
stackEntry()only attempts source enrichment when bothframe.lineandframe.columnare present.- Enriched frames use
SourceService.getPositionWithCode(...)to extract context. linesBeforedefaults to3andlinesAfterdefaults to4in the resolver implementation.
Options
ResolveOptionsInterface:
| Option | Type | Default | Description |
|---|---|---|---|
bias | Bias | Bias.BOUND | Bias used when mapping generated positions to source positions. |
linesBefore | number | 3 | Lines of context to include before the mapped source line. |
linesAfter | number | 4 | Lines of context to include after the mapped source line. |
withNativeFrames | boolean | false | Include frames where frame.native === true. |
getSource | (path: string) => SourceService | null | undefined | undefined | Provide a SourceService for a frame fileName so the resolver can add code. |
API Reference
resolveError
ts
resolveError(
error: ParsedStackTraceInterface,
options?: ResolveOptionsInterface
): ResolveMetadataInterfaceReturns:
name: error namemessage: error messagestack: formatted frames (Array<FormatStackFrameInterface>). Each frame always includesformatand may includecodewhengetSourcecan resolve the frame.
stackEntry
ts
stackEntry(
frame: StackFrameInterface,
options?: ResolveOptionsInterface
): FormatStackFrameInterface | undefinedBehavior:
- Returns
undefinedwhen the frame is filtered out (for example, native frames whenwithNativeFramesis false). - Returns a formatted entry without
codewhen source enrichment is not possible.
stackSourceEntry
ts
stackSourceEntry(
position: PositionWithCodeInterface,
frame: StackFrameInterface
): FormatStackFrameInterfaceNotes:
- This function mutates
frame(overwritingline,column,fileName, and optionallyfunctionName) with resolved source info.
formatStackLine
ts
formatStackLine(frame: StackFrameInterface): stringNotes:
- When
frame.fileNameis anhttp(s)URL,#L<line>is appended. - The
[line:column]suffix is only rendered when bothlineandcolumnare present.
