Skip to content

Commit 7f960ad

Browse files
committed
handle all error cases
1 parent 5590376 commit 7f960ad

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

lib/src/relewise.client.ts

Lines changed: 31 additions & 13 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,13 +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 {
4069
const responseMessage = await response.json();
41-
42-
if (response.ok) return responseMessage as TResponse;
4370

44-
throw responseMessage as ProblemDetailsError
45-
} catch(err) {
71+
return responseMessage as TResponse;
72+
} catch (err) {
4673
return undefined;
4774
}
4875
}
@@ -53,13 +80,4 @@ export abstract class RelewiseClient {
5380
? baseUrl.concat(joinedSegments)
5481
: baseUrl.concat('/', joinedSegments);
5582
}
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>;
6583
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,29 @@ test('Track Product View with invalid key', async() => {
170170
productId: '2',
171171
user: UserFactory.anonymous(),
172172
}).catch((e) => {
173-
console.log(e.message)
174173
expect(e).toBeDefined();
175-
expect((e as ProblemDetailsError).title ).toEqual('Unauthorized1');
176-
expect((e as any).fake ).toBeDefined();
177-
expect(e).toMatch('error');
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.')
178176
});
179177
});
180178

181179
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+
});
182187

188+
test('Track Product View without id', async() => {
183189
try {
184190
await tracker.trackProductView({
185191
productId: null,
186192
user: UserFactory.anonymous(),
187193
} as any);
188-
} catch (e) {
189-
194+
}
195+
catch (e) {
190196
expect(e).toBeDefined();
191-
expect(e).toMatch('error');
192-
193197
}
194198
});

0 commit comments

Comments
 (0)