Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript
802 B
Raw Blame
import { apiError } from "@crm/telemetry";
import { Injectable, Logger } from "@nestjs/common";
import type { OnErrorOptions, TRPCErrorHandler } from "nestjs-trpc";

@Injectable()
export class TrpcErrorHandler implements TRPCErrorHandler {
	private readonly logger = new Logger("tRPC");

	onError(opts: OnErrorOptions): void {
		const { error, type, path } = opts;
		const message = `${type} ${path ?? "<unknown>"} ${error.code}`;

		if (error.code === "INTERNAL_SERVER_ERROR") {
			this.logger.error(
				{ message, type, path, code: error.code },
				error.stack ?? String(error.cause ?? error),
			);

			apiError({
				error: error.cause ?? error,
				route: path ? `/trpc/${path}` : null,
				status: 500,
			});
			return;
		}

		this.logger.warn({ message, type, path, code: error.code });
	}
}