refactor: move toError function to utils/errors for reuse across modules

This commit is contained in:
Jakub Trávník 2026-01-02 11:54:26 +01:00
parent 4889b852be
commit 24f850b3b9
3 changed files with 4 additions and 4 deletions

View file

@ -1,8 +1,7 @@
import { Command } from "commander";
import path from "node:path";
import fs from "node:fs/promises";
const toError = (e: unknown): Error => (e instanceof Error ? e : new Error(String(e)));
import { toError } from "../../utils/errors";
type Output = ReturnType<typeof createOutput>;

View file

@ -10,6 +10,7 @@ import {
notificationDestinationsTable,
} from "../../db/schema";
import { logger } from "../../utils/logger";
import { toError } from "../../utils/errors";
import { volumeService } from "../volumes/volume.service";
import type { NotificationConfig } from "~/schemas/notifications";
import type { RepositoryConfig } from "~/schemas/restic";
@ -17,8 +18,6 @@ import type { BackendConfig } from "~/schemas/volumes";
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
const toError = (e: unknown): Error => (e instanceof Error ? e : new Error(String(e)));
const asStringArray = (value: unknown): string[] => {
if (!Array.isArray(value)) return [];
return value.filter((item): item is string => typeof item === "string");

View file

@ -18,6 +18,8 @@ export const toMessage = (err: unknown): string => {
return sanitizeSensitiveData(message);
};
export const toError = (e: unknown): Error => (e instanceof Error ? e : new Error(String(e)));
const resticErrorCodes: Record<number, string> = {
1: "Command failed: An error occurred while executing the command.",
2: "Go runtime error: A runtime error occurred in the Go program.",