Better error handling. Inspired by error handling in golang.
Deno
import { Result, must, wrap } from "https://deno.land/x/errgo/mod.ts";
Node.js
pnpm add @klapacz/errgo
import { Result, must, wrap } from "https://deno.land/x/errgo/mod.ts";
async function divide(
dividend: number,
divisor: number
): Promise<Result<number>> {
if (divisor === 0) {
return [new Error("Division by zero is not defined.")];
}
return [, dividend / divisor];
}
const [err, result] = await divide(2, 3);
if (!err) console.log(result); // typescript is smart about result being defined
// returns result or throws
const result = must(await divide(2, 3));
// wraps function which throws with `Result` interface
const [err, result] = await wrap(fetch("…"));
Deno
git tag VERSION_HERE
git push origin --tags
Node.js
# run script
deno run -A scripts/build_npm.ts VERSION_HERE
# go to output directory and publish
cd npm
npm publish