refactor(errors): extract isErrorLike and use ExecaError class in tts

This commit is contained in:
mitchell 2026-06-19 14:33:56 -04:00
parent bf5e223abf
commit 5b138df664
2 changed files with 13 additions and 8 deletions

View file

@ -1,13 +1,18 @@
import { chalkStderr } from "chalk"; import { chalkStderr } from "chalk";
import wrapAnsi from "wrap-ansi"; import wrapAnsi from "wrap-ansi";
export function handleTopLevel(err: unknown) { export interface ErrorLike {
if ( message: string;
}
export const isErrorLike = (err: unknown): err is ErrorLike =>
typeof err === "object" && typeof err === "object" &&
err !== null && err !== null &&
"message" in err && "message" in err &&
typeof err.message === "string" typeof err.message === "string";
) {
export function handleTopLevel(err: unknown) {
if (isErrorLike(err)) {
if (process.stderr.isTTY) { if (process.stderr.isTTY) {
const [columns] = process.stderr.getWindowSize(); const [columns] = process.stderr.getWindowSize();
console.error(wrapAnsi(chalkStderr.red(err.message), columns)); console.error(wrapAnsi(chalkStderr.red(err.message), columns));

View file

@ -8,7 +8,7 @@ import consumers from "node:stream/consumers";
import util from "node:util"; import util from "node:util";
import { Readability } from "@mozilla/readability"; import { Readability } from "@mozilla/readability";
import chalk from "chalk"; import chalk from "chalk";
import { execa } from "execa"; import { ExecaError, execa } from "execa";
import { Window } from "happy-dom"; import { Window } from "happy-dom";
import wrapAnsi from "wrap-ansi"; import wrapAnsi from "wrap-ansi";
import { handleTopLevel } from "./errors"; import { handleTopLevel } from "./errors";
@ -151,7 +151,7 @@ async function main() {
try { try {
await execa({ cwd, shell: true })`sox ${files} output.wav`; await execa({ cwd, shell: true })`sox ${files} output.wav`;
} catch (err) { } catch (err) {
if (err instanceof Object && "code" in err && err.code === "ENOENT") { if (err instanceof ExecaError && err.code === "ENOENT") {
console.error("Sox not found. Unable to combine outputs."); console.error("Sox not found. Unable to combine outputs.");
console.log(chalk.yellow(`Audio available in ${cwd}`)); console.log(chalk.yellow(`Audio available in ${cwd}`));
return; return;