All files / apps/web/src/handlers errors.ts

100% Statements 7/7
100% Branches 10/10
100% Functions 4/4
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29        70x 183x     184x       226x         184x       70x       402x      
import type { TRPCError } from "@trpc/server";
import type { z } from "zod";
import { ZodError } from "zod";
 
const formatZodErrors = (error: z.ZodError) =>
	`Zod error${error.issues.length <= 1 ? "" : "s"}\n\n${error.issues
		.map((issue) => {
			const path =
				issue.path.length === 0
					? "<root>"
					: issue.path
							.map<string>((key) =>
								typeof key === "number" ? `[${key}]` : String(key),
							)
							.filter(Boolean)
							.join(".")
							.replaceAll(".[", "[");
			return `At "${path}": ${issue.message}`;
		})
		.join("\n\n")}`;
 
export const formatErrorMessage = (
	error: TRPCError,
	fallbackMessage: string,
) =>
	error.code === "BAD_REQUEST" && error.cause instanceof ZodError
		? formatZodErrors(error.cause)
		: fallbackMessage;