The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Surface the real ClickHouse error when an insert fails (#24189)
Thomas Trompette2dd0af375508
diff --git a/packages/twenty-server/src/database/clickHouse/clickHouse.service.ts b/packages/twenty-server/src/database/clickHouse/clickHouse.service.tsindex f5fe0829956..839603f3a7e 100644--- a/packages/twenty-server/src/database/clickHouse/clickHouse.service.ts+++ b/packages/twenty-server/src/database/clickHouse/clickHouse.service.ts@@ -18,6 +18,10 @@ export type ClickHouseInsertOptions = {asyncInsertBusyTimeoutMaxMs?: number;};+export type ClickHouseInsertResult =+ | { success: true }+ | { success: false; error: Error };+@Injectable()export class ClickHouseService implements OnModuleInit, OnModuleDestroy {private mainClient: ClickHouseClient | undefined;@@ -136,14 +140,19 @@ export class ClickHouseService implements OnModuleInit, OnModuleDestroy {table: string,values: T[],options: ClickHouseInsertOptions = {},- ): Promise<{ success: boolean }> {+ ): Promise<ClickHouseInsertResult> {try {const client = options.clientId? await this.connectToClient(options.clientId): this.mainClient;if (!client) {- return { success: false };+ return {+ success: false,+ error: new Error(+ `No ClickHouse client available${options.clientId ? ` for client ${options.clientId}` : ''}`,+ ),+ };}await this.insertInChunks(client, table, values, {@@ -156,7 +165,13 @@ export class ClickHouseService implements OnModuleInit, OnModuleDestroy {} catch (err) {this.logger.error('Error inserting data into ClickHouse', err);- return { success: false };+ return {+ success: false,+ error:+ err instanceof Error+ ? err+ : Object.assign(new Error(String(err)), { cause: err }),+ };}}diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.spec.ts b/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.spec.tsindex ca51de72292..7265cec1a6c 100644--- a/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.spec.ts@@ -68,8 +68,20 @@ describe('ClickHouseEventSink', () => {});it('throws when a ClickHouse insert fails so the consumer retries', async () => {- insert.mockResolvedValue({ success: false });+ insert.mockResolvedValue({ success: false, error: new Error('boom') });await expect(sink.write([makePageview('a')])).rejects.toThrow();});++ it('surfaces the underlying ClickHouse error in the message and cause', async () => {+ const error = new Error('socket hang up');++ insert.mockResolvedValue({ success: false, error });++ await expect(sink.write([makePageview('a')])).rejects.toMatchObject({+ message:+ 'Failed to insert 1 pageview row(s) into ClickHouse: socket hang up',+ cause: error,+ });+ });});diff --git a/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.ts b/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.tsindex e154fa3f430..fab5aae3d72 100644--- a/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.ts+++ b/packages/twenty-server/src/engine/core-modules/event-logs/ingest/clickhouse-event.sink.ts@@ -48,8 +48,11 @@ export class ClickHouseEventSink implements EventSink {);if (!result.success) {- throw new Error(- `Failed to insert ${rows.length} ${table} row(s) into ClickHouse`,+ throw Object.assign(+ new Error(+ `Failed to insert ${rows.length} ${table} row(s) into ClickHouse: ${result.error.message}`,+ ),+ { cause: result.error },);}}),