Library writes raw stack traces to console.error, which corrupts inline-viewport TUIs; profileStep is unguarded and the null return is undocumented
Environment
options.debug already exists and gates some of this, but the failure paths above are unconditional.
Problem 2 — profileStep lets exceptions escape
// dist/defuddle.js:685-692
const profileStep = (name, fn) => {
if (!doProfile)
return fn(); // line 687 — direct call, no try/catch
const t = performance.now();
const result = fn(); // line 689 — same
profile[name] = Math.round(performance.now() - t);
return result;
};Every step runs through this wrapper, so a throw from any of them (cloneDocument, flattenShadowRoots, findMainContent, standardizeContent, …) unwinds straight to the outer catch, losing which step failed. A DOM traversal that throws — e.g. linkedom query on a cloned document whose nodes were dropped while sanitising a bot-blocked or empty page — takes the whole extraction down.
Problem 3 — silent null return is a footgun
The outer catch returns null without telling the caller anything. Callers reasonably assume an object:
const result = Defuddle(document, url, { markdown: true });
return result.content; // TypeError: Cannot read properties of nullThat TypeError replaces the real cause, so the actual extraction error is only visible in the console line above — exactly the output that a TUI host cannot render.
Suggested changes
Never write to the console from library code. Accept a logger, default to silent:
export interface DefuddleOptions {
onLog?: (level: 'debug' | 'warn' | 'error', message: string, error?: unknown) => void;
// or: logger?: { debug(...a): void; warn(...a): void; error(...a): void };
}With no logger supplied, drop the message. Keep the existing
debugflag as sugar over the same channel.Guard
profileStepso the failing step is named:const profileStep = (name, fn) => {
const t = doProfile ? performance.now() : 0;
try {
const result = fn();
if (doProfile) profile[name] = Math.round(performance.now() - t);
return result;
} catch (error) {
this.log?.('error', `defuddle: step "${name}" failed`, error);
throw new Error(`defuddle: step "${name}" failed: ${(error as Error)?.message ?? error}`, { cause: error });
}
};Either throw or document the
null. Preferred: throw a typedDefuddleError. Ifnullmust stay for compatibility, mark the return typeDefuddleResult | nullin the public types so callers are forced to check.
Not asking for
Behaviour changes to extraction quality or selectors — this is only about error reporting and the null contract.
Happy to submit a PR for 1 and 2 against 0.19.x if that helps; the reporter is the maintainer of a pi plugin (@pi-plugins/webfetch) that calls Defuddle(document, url, { markdown: true }) and wraps it in its own error type, so we can validate the fix end to end.