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 wrapAnsi from "wrap-ansi";
export interface ErrorLike {
message: string;
}
export const isErrorLike = (err: unknown): err is ErrorLike =>
typeof err === "object" &&
err !== null &&
"message" in err &&
typeof err.message === "string";
export function handleTopLevel(err: unknown) {
if (
typeof err === "object" &&
err !== null &&
"message" in err &&
typeof err.message === "string"
) {
if (isErrorLike(err)) {
if (process.stderr.isTTY) {
const [columns] = process.stderr.getWindowSize();
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 { Readability } from "@mozilla/readability";
import chalk from "chalk";
import { execa } from "execa";
import { ExecaError, execa } from "execa";
import { Window } from "happy-dom";
import wrapAnsi from "wrap-ansi";
import { handleTopLevel } from "./errors";
@ -151,7 +151,7 @@ async function main() {
try {
await execa({ cwd, shell: true })`sox ${files} output.wav`;
} 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.log(chalk.yellow(`Audio available in ${cwd}`));
return;