Skip to content

Commit b22d0ef

Browse files
authored
Merge pull request #78 from Relewise/feat-improve-error-handling
feat: improve error handling
2 parents d4a46a9 + 8194508 commit b22d0ef

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/client/src/relewise.client.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ export interface RelewiseRequestOptions {
1010
}
1111

1212
export class ProblemDetailsError extends Error {
13-
private _details?: HttpProblemDetails;
13+
private _details?: HttpProblemDetails | null;
1414

15-
public get details(): HttpProblemDetails | undefined {
15+
public get details(): HttpProblemDetails | undefined | null {
1616
return this._details;
1717
}
1818

19-
constructor(message: string, details?: HttpProblemDetails) {
19+
constructor(message: string, details?: HttpProblemDetails | null) {
2020
super(message);
2121
this._details = details;
2222
}
@@ -67,18 +67,20 @@ export abstract class RelewiseClient {
6767
});
6868

6969
if (!response.ok) {
70-
let responseMessage = null;
70+
let responseMessage: HttpProblemDetails | null = null;
71+
7172
try {
7273
responseMessage = await response.json();
7374
} catch (_) {
7475
}
7576

76-
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);
77+
const details = responseMessage?.detail ? `Details: ${responseMessage.detail}\n` : '';
78+
79+
throw new ProblemDetailsError(`Error when calling the Relewise API.\n\nTitle: ${response.statusText}\nStatus: ${response.status}\n${details}\nRead more in the details property if there is error response or look in the network tab.`, responseMessage);
7780
}
7881

7982
try {
8083
const responseMessage = await response.json();
81-
8284
return responseMessage as TResponse;
8385
} catch (err) {
8486
return undefined;

packages/client/tests/integration-tests/tracker.integration.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ test('Track Product View with invalid key', async() => {
172172
}).catch((e) => {
173173
expect(e).toBeDefined();
174174
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.')
175+
expect(e.message).toContain('Error when calling the Relewise API.')
176+
expect(e.message).toContain('Title: Unauthorized')
177+
expect(e.message).toContain('Status: 401')
176178
});
177179
});
178180

0 commit comments

Comments
 (0)