Produces reports with one set of options. Construct once, reuse for every caught object.

Example

import * as Corj from 'caught-object-report-json';

const maker = new Corj.CorjMaker({ maxReportSize: 16_000, maxDepth: 2, redact: { keys: ['password'] } });

try {
await job.run();
} catch (caught: unknown) {
logger.error({ jobId: job.id, error: maker.makeReport(caught, { context: { attempt: 1 } }) });
}

Constructors

  • Invalid options throw a TypeError or RangeError; unknown option names are rejected too.

    Parameters

    Returns CorjMaker

    Example

    new Corj.CorjMaker({ maxReportSize: 100 });
    // RangeError: maxReportSize must be a safe integer >= 512, or null to disable the limit

Properties

ctx: Ctx
options: CorjOptions

Methods

  • Records are per call. A caught object may re-enter this maker, so the previous list is restored.

    Type Parameters

    • T

    Parameters

    • produce: (() => T)
        • (): T
        • Returns T

    Returns T

  • The fingerprint alone: discovery and node fields, without as_json, the context or the limiter. Building no as_json means a caught object whose .toCorjAsJson() or getters change it as they run can fingerprint differently here than it does inside a report.

    requireStack returns a fingerprint only when fingerprintParts names stack and the root's stack still carries frames once it has been read and redacted; anything else is undefined, because a hash of the caught value's own text is one a reader who can guess that text can confirm.

    Parameters

    • caught: unknown
    • Optional options: {
          requireStack?: boolean;
      }
      • Optional requireStack?: boolean

    Returns undefined | string

    Example

    maker.makeFingerprint(err) === maker.makeFingerprint(err); // true, 'fp1_' + 32 hex characters
    maker.makeFingerprint('boom', { requireStack: true }); // undefined: nothing frame-backed to hash
  • The bounded JSON form of any value, under this maker's inspection and redact options. A named root such as "$context" starts a separate document, so a rule written for one never reaches another.

    Parameters

    • value: unknown
    • options: {
          maxSize?: null | number;
          root?: string;
      } = {}
      • Optional maxSize?: null | number
      • Optional root?: string

    Returns CorjJsonView

    Example

    const maker = new Corj.CorjMaker({ redact: { keys: ['password'] } });
    maker.makeJsonView({ user: 'ada', password: 'p' }, { root: '$request', maxSize: 512 });
    // { value: { user: 'ada', password: '[redacted]' }, truncated: false, errors: [] }
  • The report as one object. Never throws because of caught; an invalid call input throws a TypeError before any work is done.

    Parameters

    Returns CorjReport

    Example

    const report = maker.makeReport(caught, { occurrenceId: requestId, context: { route: '/checkout' } });
    // { occurrence_id: 'req-42', fingerprint: 'fp1_…', stack: [...], children?: [...], context: {...}, v: 'corj/v0.15' }
  • The root as the first element followed by every child; nodes link to each other by child_ids.

    Parameters

    Returns CorjReportNode[]

    Example

    maker.makeReportArray(new Error('outer', { cause: new Error('inner') })).map((node) => node.path);
    // [ '$', '$.cause' ]
  • This maker's redact policy applied to one string a consumer emits itself, such as a log line of its own. Without a policy it is the identity.

    Parameters

    • text: string
    • where: {
          path?: string;
          reportKey?: CorjReportKey;
          sourceProperty?: string;
      } = {}
      • Optional path?: string
      • Optional reportKey?: CorjReportKey
      • Optional sourceProperty?: string

    Returns string

    Example

    const maker = new Corj.CorjMaker({ redact: { patterns: [/sk-[a-z0-9]+/g] } });
    maker.scrubText('token sk-123 used', { path: '$audit' }); // 'token [redacted] used'
  • A new maker with these options applied on top of this maker's options. The original is unchanged.

    Parameters

    Returns CorjMaker

    Example

    const base = new Corj.CorjMaker({ redact: { keys: ['password'] } });
    const forModel = base.withOptions({ maxReportSize: 4096, metadata: false }); // keeps the redact policy