@streetjs/diagnostics
v1.0.0
Published
StreetJS structured diagnostics reporter: serializes errors into clean JSON DiagnosticEvents on stderr, strips Node-internal stack frames, and emits a 'diagnostic' event. Zero runtime dependencies.
Maintainers
Readme
@streetjs/diagnostics
The StreetJS structured diagnostics reporter: turns any thrown value into a
clean, structured DiagnosticEvent, emits it on a 'diagnostic' event, and
writes the JSON line to process.stderr. Zero runtime dependencies, ESM.
This is the standalone home of the reporter that also backs the streetjs
framework. The framework re-exports this package, so there is a single source of
truth.
Install
npm install @streetjs/diagnosticsUsage
import { diagnosticsReporter } from '@streetjs/diagnostics';
// Ship structured diagnostics to your sink of choice.
diagnosticsReporter.on('diagnostic', (event) => {
metrics.increment('errors', { class: event.errorClass });
});
try {
doWork();
} catch (err) {
diagnosticsReporter.report(err, correlationId);
}Use the shared diagnosticsReporter singleton, or construct your own
new DiagnosticsReporter() (it extends EventEmitter).
The event
report(err, correlationId?) produces a DiagnosticEvent:
interface DiagnosticEvent {
level: 'error' | 'warn';
errorClass: string; // constructor name, "StringError", or "UnknownError"
message: string;
stack: string[]; // cleaned frames only
correlationId?: string;
ts: string; // ISO 8601
}Error→errorClassis the constructor name (subclasses preserved),messageiserr.message, andstackis cleaned.string→errorClassis"StringError",messageis the string.- anything else →
errorClassis"UnknownError",messageisString(err).
Stack cleaning
Only real at … frames are kept; blank/noise lines and Node-internal frames
(node:internal, node_modules/node) are stripped, so the stack shows your code
rather than runtime plumbing.
Example
A complete runnable example lives in
src/examples/integration.ts:
npm run example -w packages/diagnosticsLicense
MIT — see LICENSE.
