Skip to content

Commit ff5292c

Browse files
logger.throw
1 parent 5d140dc commit ff5292c

File tree

21 files changed

+259
-114
lines changed

21 files changed

+259
-114
lines changed

packages/server/destinations/aws/src/firehose/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ export const destinationFirehose: Destination = {
1111

1212
config: {},
1313

14-
async init({ config: partialConfig, env }) {
14+
async init({ config: partialConfig, env, logger }) {
1515
const config = getConfig(partialConfig, env);
1616

17-
if (!isSameType(config.settings, {} as Settings)) return false;
17+
if (!isSameType(config.settings, {} as Settings))
18+
logger.throw('Config settings invalid');
1819

1920
return config;
2021
},

packages/server/destinations/datamanager/src/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type { Config, Settings, PartialConfig } from './types';
2-
import { throwError } from '@walkeros/core';
2+
import type { Logger } from '@walkeros/core';
33

4-
export function getConfig(partialConfig: PartialConfig = {}): Config {
4+
export function getConfig(
5+
partialConfig: PartialConfig = {},
6+
logger: Logger.Instance,
7+
): Config {
58
const settings = (partialConfig.settings || {}) as Partial<Settings>;
69
const { destinations } = settings;
710

811
if (!destinations || destinations.length === 0)
9-
throwError('Config settings destinations missing or empty');
12+
logger.throw('Config settings destinations missing or empty');
1013

1114
const settingsConfig: Settings = {
1215
...settings,

packages/server/destinations/datamanager/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ export const destinationDataManager: DestinationInterface = {
1010

1111
config: {},
1212

13-
async init({ config: partialConfig, env }) {
14-
const config = getConfig(partialConfig);
13+
async init({ config: partialConfig, env, logger }) {
14+
const config = getConfig(partialConfig, logger);
1515

1616
if (!config.settings) {
17-
throw new Error('Settings required for Data Manager destination');
17+
logger.throw('Settings required for Data Manager destination');
1818
}
1919

2020
if (
2121
!config.settings.destinations ||
2222
config.settings.destinations.length === 0
2323
) {
24-
throw new Error('At least one destination required in settings');
24+
logger.throw('At least one destination required in settings');
2525
}
2626

2727
try {
@@ -35,7 +35,7 @@ export const destinationDataManager: DestinationInterface = {
3535
},
3636
};
3737
} catch (error) {
38-
throw new Error(
38+
logger.throw(
3939
`Data Manager authentication failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
4040
);
4141
}

packages/server/destinations/gcp/src/bigquery/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import type { Config, Env, PartialConfig, Settings } from './types';
22
import type { BigQueryOptions } from '@google-cloud/bigquery';
3-
import { throwError } from '@walkeros/core';
3+
import type { Logger } from '@walkeros/core';
44
import { BigQuery } from '@google-cloud/bigquery';
55

66
export function getConfig(
77
partialConfig: PartialConfig = {},
8-
env?: Env,
8+
env: Env | undefined,
9+
logger: Logger.Instance,
910
): Config {
1011
const settings = partialConfig.settings || ({} as Settings);
1112
const { projectId, bigquery } = settings;
1213
let { client, location, datasetId, tableId } = settings;
1314

14-
if (!projectId) throwError('Config settings projectId missing');
15+
if (!projectId) logger.throw('Config settings projectId missing');
1516

1617
location = location || 'EU';
1718
datasetId = datasetId || 'walkeros';

packages/server/destinations/gcp/src/bigquery/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const destinationBigQuery: Destination = {
1010

1111
config: {},
1212

13-
async init({ config: partialConfig, env }) {
14-
const config = getConfig(partialConfig, env);
13+
async init({ config: partialConfig, env, logger }) {
14+
const config = getConfig(partialConfig, env, logger);
1515

1616
return config;
1717
},

packages/server/destinations/meta/src/config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type { Config, Settings, PartialConfig } from './types';
2-
import { throwError } from '@walkeros/core';
2+
import type { Logger } from '@walkeros/core';
33

4-
export function getConfig(partialConfig: PartialConfig = {}): Config {
4+
export function getConfig(
5+
partialConfig: PartialConfig = {},
6+
logger: Logger.Instance,
7+
): Config {
58
const settings = (partialConfig.settings || {}) as Partial<Settings>;
69
const { accessToken, pixelId } = settings;
710

8-
if (!accessToken) throwError('Config settings accessToken missing');
9-
if (!pixelId) throwError('Config settings pixelId missing');
11+
if (!accessToken) logger.throw('Config settings accessToken missing');
12+
if (!pixelId) logger.throw('Config settings pixelId missing');
1013

1114
const settingsConfig: Settings = {
1215
...settings,

packages/server/destinations/meta/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const destinationMeta: Destination = {
1010

1111
config: {},
1212

13-
async init({ config: partialConfig }) {
14-
const config = getConfig(partialConfig);
13+
async init({ config: partialConfig, logger }) {
14+
const config = getConfig(partialConfig, logger);
1515
return config;
1616
},
1717

packages/web/destinations/api/src/index.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ describe('Destination API', () => {
1515
const testEnv = clone(examples.env.push);
1616
testEnv.sendWeb = mockSendWeb;
1717

18+
// Mock logger
19+
const mockLogger = createMockLogger();
20+
1821
beforeEach(async () => {
1922
jest.clearAllMocks();
2023

@@ -26,21 +29,23 @@ describe('Destination API', () => {
2629
});
2730

2831
test('init', () => {
29-
// Test with no URL - should not call sendWeb
30-
destination.push(event, {
31-
collector: {} as Collector.Instance,
32-
config: {},
33-
env: testEnv,
34-
logger: createMockLogger(),
35-
});
32+
// Test with no URL - should throw error
33+
expect(() =>
34+
destination.push(event, {
35+
collector: {} as Collector.Instance,
36+
config: {},
37+
env: testEnv,
38+
logger: mockLogger,
39+
}),
40+
).toThrow('Config settings url missing');
3641
expect(mockSendWeb).not.toHaveBeenCalled();
3742

3843
// Test with URL - should call sendWeb
3944
destination.push(event, {
4045
collector: {} as Collector.Instance,
4146
config: { settings: { url } },
4247
env: testEnv,
43-
logger: createMockLogger(),
48+
logger: mockLogger,
4449
});
4550
expect(mockSendWeb).toHaveBeenCalledTimes(1);
4651

@@ -64,7 +69,7 @@ describe('Destination API', () => {
6469
settings: { url },
6570
},
6671
env: customEnv,
67-
logger: createMockLogger(),
72+
logger: mockLogger,
6873
});
6974

7075
expect(customSendWeb).toHaveBeenCalledTimes(1);
@@ -85,7 +90,7 @@ describe('Destination API', () => {
8590
settings: { url, transform: () => 'transformed' },
8691
},
8792
env: testEnv,
88-
logger: createMockLogger(),
93+
logger: mockLogger,
8994
});
9095
expect(mockSendWeb).toHaveBeenCalledWith(
9196
url,
@@ -101,7 +106,7 @@ describe('Destination API', () => {
101106
settings: { url, headers: { foo: 'bar' } },
102107
},
103108
env: testEnv,
104-
logger: createMockLogger(),
109+
logger: mockLogger,
105110
});
106111
expect(mockSendWeb).toHaveBeenCalledWith(
107112
url,
@@ -119,7 +124,7 @@ describe('Destination API', () => {
119124
settings: { url, method: 'POST' },
120125
},
121126
env: testEnv,
122-
logger: createMockLogger(),
127+
logger: mockLogger,
123128
});
124129
expect(mockSendWeb).toHaveBeenCalledWith(
125130
url,
@@ -138,7 +143,7 @@ describe('Destination API', () => {
138143
mapping: examples.mapping.config,
139144
},
140145
env: testEnv,
141-
logger: createMockLogger(),
146+
logger: mockLogger,
142147
});
143148

144149
expect(mockSendWeb).toHaveBeenCalledWith(

packages/web/destinations/api/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const destinationAPI: Destination = {
1010

1111
config: {},
1212

13-
push(event, { config, mapping, data, env }) {
13+
push(event, { config, mapping, data, env, logger }) {
1414
const { settings } = config;
1515
const {
1616
url,
@@ -20,15 +20,15 @@ export const destinationAPI: Destination = {
2020
transport = 'fetch',
2121
} = settings || {};
2222

23-
if (!url) return;
23+
if (!url) logger.throw('Config settings url missing');
2424

2525
const eventData = isDefined(data) ? data : event;
2626
const body = transform
2727
? transform(eventData, config, mapping) // Transform event data
2828
: JSON.stringify(eventData);
2929

3030
const sendWebFn = (env as Env)?.sendWeb || sendWeb;
31-
sendWebFn(url, body, { headers, method, transport });
31+
sendWebFn(url!, body, { headers, method, transport });
3232
},
3333
};
3434

packages/web/destinations/gtag/src/__tests__/ads.test.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
import { initAds, pushAdsEvent } from '../ads';
22
import { examples } from '../dev';
3-
import { clone } from '@walkeros/core';
3+
import { clone, createMockLogger } from '@walkeros/core';
44
import type { AdsSettings, AdsMapping } from '../types';
55

66
describe('Google Ads Implementation', () => {
77
const mockGtag = jest.fn();
88
const mockEnv = clone(examples.env.push);
99
mockEnv.window.gtag = mockGtag;
1010

11+
// Create a mock logger that actually throws
12+
const createThrowingLogger = () => {
13+
const logger = createMockLogger();
14+
logger.throw = (message: string) => {
15+
throw new Error(message);
16+
};
17+
return logger;
18+
};
19+
1120
beforeEach(() => {
1221
jest.clearAllMocks();
1322
});
1423

1524
describe('initAds', () => {
16-
it('should return early if no conversionId', () => {
25+
it('should throw error if no conversionId', () => {
1726
const settings: AdsSettings = { conversionId: '' };
27+
const logger = createThrowingLogger();
1828

19-
initAds(settings, undefined, mockEnv);
20-
21-
expect(mockGtag).not.toHaveBeenCalled();
29+
expect(() => initAds(settings, undefined, mockEnv, logger)).toThrow(
30+
'Config settings ads.conversionId missing',
31+
);
2232
});
2333

2434
it('should initialize Ads with basic settings', () => {
@@ -72,10 +82,20 @@ describe('Google Ads Implementation', () => {
7282
currency: 'EUR',
7383
};
7484

75-
it('should return early if no mapping name', () => {
76-
pushAdsEvent(mockEvent as any, settings, {}, {}, undefined, mockEnv);
77-
78-
expect(mockGtag).not.toHaveBeenCalled();
85+
it('should throw error if no mapping name', () => {
86+
const logger = createThrowingLogger();
87+
88+
expect(() =>
89+
pushAdsEvent(
90+
mockEvent as any,
91+
settings,
92+
{},
93+
{},
94+
undefined,
95+
mockEnv,
96+
logger,
97+
),
98+
).toThrow('Config mapping ads.label missing');
7999
});
80100

81101
it('should push conversion event with correct parameters', () => {
@@ -190,12 +210,21 @@ describe('Google Ads Implementation', () => {
190210
});
191211
});
192212

193-
it('should return early when neither mapping.label nor mappingName is provided', () => {
213+
it('should throw error when neither mapping.label nor mappingName is provided', () => {
194214
const mapping: AdsMapping = {};
195-
196-
pushAdsEvent(mockEvent as any, settings, mapping, {}, undefined, mockEnv);
197-
198-
expect(mockGtag).not.toHaveBeenCalled();
215+
const logger = createThrowingLogger();
216+
217+
expect(() =>
218+
pushAdsEvent(
219+
mockEvent as any,
220+
settings,
221+
mapping,
222+
{},
223+
undefined,
224+
mockEnv,
225+
logger,
226+
),
227+
).toThrow('Config mapping ads.label missing');
199228
});
200229
});
201230
});

0 commit comments

Comments
 (0)