Skip to content

Commit 6b035ff

Browse files
authored
Merge pull request #37 from Relewise/feat/improve-error-handling
feat: improve error handling
2 parents 635737b + 4eba2f7 commit 6b035ff

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

lib/src/relewise.client.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ export interface RelewiseClientOptions {
55
serverUrl?: string;
66
}
77

8+
export class ProblemDetailsError extends Error {
9+
private _details?: HttpProblemDetails;
10+
11+
public get details(): HttpProblemDetails | undefined {
12+
return this._details;
13+
}
14+
15+
constructor(message: string, details?: HttpProblemDetails) {
16+
super(message);
17+
this._details = details;
18+
}
19+
}
20+
21+
export interface HttpProblemDetails {
22+
type: string;
23+
title: string;
24+
status: number;
25+
traceId: string;
26+
detail?: string;
27+
errors?: Record<string, string>;
28+
}
29+
830
export abstract class RelewiseClient {
931
private readonly _serverUrl: string = 'https://api.relewise.com';
1032
private readonly _urlPath: string = 'v1';
@@ -36,9 +58,18 @@ export abstract class RelewiseClient {
3658
body: JSON.stringify(data),
3759
});
3860

61+
if (!response.ok) {
62+
let responseMessage = null;
63+
try { responseMessage = await response.json(); } catch (_) { console.log(responseMessage)}
64+
65+
throw new ProblemDetailsError('Error when calling the Relewise API. Read more in the details property if there is error response or look in the network tab.', responseMessage);
66+
}
67+
3968
try {
40-
return await response.json() as TResponse;
41-
} catch(err) {
69+
const responseMessage = await response.json();
70+
71+
return responseMessage as TResponse;
72+
} catch (err) {
4273
return undefined;
4374
}
4475
}

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

Lines changed: 42 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,49 @@ 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+
expect(e).toBeDefined();
174+
expect((e as ProblemDetailsError).details?.title).toEqual('Unauthorized');
175+
expect(e.message).toEqual('Error when calling the Relewise API. Read more in the details property if there is error response or look in the network tab.')
176+
});
177+
});
178+
179+
test('Track Product View without id', async() => {
180+
await expect(async() => {
181+
return await tracker.trackProductView({
182+
productId: null,
183+
user: UserFactory.anonymous(),
184+
} as any)
185+
}).rejects.toThrow();
186+
});
187+
188+
test('Track Product View without id', async() => {
189+
try {
190+
await tracker.trackProductView({
191+
productId: null,
192+
user: UserFactory.anonymous(),
193+
} as any);
194+
}
195+
catch (e) {
196+
expect(e).toBeDefined();
197+
}
163198
});

0 commit comments

Comments
 (0)