Skip to main content

Function: safeCause()

safeCause(err): unknown

Defined in: src/core/errors.ts:184

A copy of err that is safe to keep as an error's cause: its name, message and stack (all through redactSecrets, so a user transport's own error text is covered too) and its code / errno / syscall — nothing else. A raw HTTP-client error must never be attached as-is: an axios error carries the request config, so its Authorization header, cookies and a POSTed login form (password included) would surface in util.inspect(err) or a logged stack. An SdvError is kept (its own cause went through this when it was built).

Parameters​

err​

unknown

Anything caught: an Error, an axios / fetch client error, a string, undefined.

Returns​

unknown

err itself when it is undefined, null or an SdvError; otherwise a new plain Error carrying only the redacted name, message and stack plus a string or number code / errno / syscall. A non-object err becomes Error(String(err)).

Example​

import { AssetFetchError } from 'sportsdataverse';
import { safeCause } from './core/errors.js'; // not re-exported from the package root

try {
await myHttpClient.get(url);
} catch (err) {
throw new AssetFetchError('my_family: fetch failed', { url, cause: safeCause(err) });
}

Remarks​

SdvError's constructor already applies this to options.cause, so an error built through any sportsdataverse error class never needs it called explicitly; the built-in transports call it on the error they reject with.