@ocubist/error-alchemy
Version:
A powerful Node.js error-handling-framework with custom error types for easy debugging, enhanced error management, strong zod-support and useful quality-of-life-functionality for asserting and validating values.
29 lines (26 loc) • 880 B
text/typescript
import { DetectorFunction, Transmuter, TransmuterFunction } from "./types";
import { MysticError } from "../../transmuted-errors/MysticError";
/**
* Crafts a new transmuter with the given detector and transmuter functions.
*
* @template T
* @param {DetectorFunction} detectorFunction - The function to detect errors.
* @param {TransmuterFunction<T>} transmuterFunction - The function to transmute errors.
* @returns {Transmuter<T>} The crafted transmuter.
*/
export const craftErrorTransmuter = <T = any>(
detectorFunction: DetectorFunction,
transmuterFunction: TransmuterFunction<T>
): Transmuter<T> => {
const newTransmuter: Transmuter<T> = {
detect: detectorFunction,
transmute: transmuterFunction,
execute(err) {
if (this.detect(err)) {
return this.transmute(err as T);
}
return err;
},
};
return newTransmuter;
};