Skip to content

Commit 5590376

Browse files
committed
feat: improve error handling
1 parent 635737b commit 5590376

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

lib/src/relewise.client.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export abstract class RelewiseClient {
3737
});
3838

3939
try {
40-
return await response.json() as TResponse;
40+
const responseMessage = await response.json();
41+
42+
if (response.ok) return responseMessage as TResponse;
43+
44+
throw responseMessage as ProblemDetailsError
4145
} catch(err) {
4246
return undefined;
4347
}
@@ -49,4 +53,13 @@ export abstract class RelewiseClient {
4953
? baseUrl.concat(joinedSegments)
5054
: baseUrl.concat('/', joinedSegments);
5155
}
56+
}
57+
58+
export interface ProblemDetailsError {
59+
type: string;
60+
title: string;
61+
status: number;
62+
traceId: string;
63+
detail?: string;
64+
errors?: Record<string, string>;
5265
}

lib/tests/integration-tests/tracker.integration.test.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { DataValueFactory, Tracker, UserFactory } from '../../src';
1+
import { error } from 'console';
2+
import { DataValueFactory, ProblemDetailsError, Tracker, UserFactory } from '../../src';
23
import { test, expect } from '@jest/globals'
34

45
const { npm_config_API_KEY: API_KEY, npm_config_DATASET_ID: DATASET_ID, npm_config_SERVER_URL: SERVER_URL } = process.env;
@@ -19,10 +20,10 @@ test('Track Order', async() => {
1920
productId: '2',
2021
quantity: 1,
2122
variantId: 'v1',
22-
} ],
23+
}],
2324
subtotal: {
2425
amount: 100,
25-
currency: 'DKK',
26+
currency: 'DKK',
2627
},
2728
orderNumber: '',
2829
trackingNumber: '',
@@ -50,7 +51,7 @@ test('Track Cart', async() => {
5051
],
5152
subtotal: {
5253
amount: 100,
53-
currency: 'DKK',
54+
currency: 'DKK',
5455
},
5556
user: UserFactory.anonymous(),
5657
data: { 'basketId': DataValueFactory.string('basketid') },
@@ -149,15 +150,45 @@ test('Track Search Term', async() => {
149150
});
150151

151152
test('Track User Update', async() => {
152-
const user = UserFactory.byTemporaryId('tempId', {
153-
email: 'integrationtests@relewise.com',
153+
const user = UserFactory.byTemporaryId('tempId', {
154+
email: 'integrationtests@relewise.com',
154155
identifiers: {
155156
'emailIntegrationId': 'abc',
156-
}});
157+
},
158+
});
157159

158160
const result = await tracker.trackUserUpdate({
159161
user: user,
160162
});
161163

162164
expect(result).toBeUndefined();
165+
});
166+
167+
test('Track Product View with invalid key', async() => {
168+
169+
await new Tracker(DATASET_ID!, '12').trackProductView({
170+
productId: '2',
171+
user: UserFactory.anonymous(),
172+
}).catch((e) => {
173+
console.log(e.message)
174+
expect(e).toBeDefined();
175+
expect((e as ProblemDetailsError).title ).toEqual('Unauthorized1');
176+
expect((e as any).fake ).toBeDefined();
177+
expect(e).toMatch('error');
178+
});
179+
});
180+
181+
test('Track Product View without id', async() => {
182+
183+
try {
184+
await tracker.trackProductView({
185+
productId: null,
186+
user: UserFactory.anonymous(),
187+
} as any);
188+
} catch (e) {
189+
190+
expect(e).toBeDefined();
191+
expect(e).toMatch('error');
192+
193+
}
163194
});

0 commit comments

Comments
 (0)