API reference › @evolu/common › Error › defectToError
function defectToError(reported: unknown): Error;
Defined in: packages/common/src/Error.ts:166
Converts a reported defect to an Error that a host error reporter shows
readably.
Hosts such as browsers and React Native show a reported value that is not an
Error only as text such as "[object Object]", and a worker's error reaches
its page, including an error tracker listening there, as that text alone. A
panic reports a plain AbortError, so its defect is converted instead.
An Error is returned as it is, including one from another realm, such as an
iframe. A DOMException is described in an Error with its name and
message, because Chromium reports one from a worker without them. Any other
value is described in an Error whose cause is what was reported.
Platform createRun adapters use it for their default reporting. A custom ReportDefect can use it too, such as before passing a defect to an error tracker.
Example
import {
assertEqual,
assertSame,
createRun,
defectToError,
} from "@evolu/common";
const errors: Array<Error> = [];
await using run = createRun({
reportDefect: (reported) => {
errors.push(defectToError(reported));
},
});
const defect = new Error("boom");
run.panic(defect);
assertSame(errors[0], defect);
assertEqual(
defectToError({ type: "UnexpectedState" }).message,
'Defect: {"type":"UnexpectedState"}',
);