diff --git a/packages/client/src/relewise.client.ts b/packages/client/src/relewise.client.ts index 0630322..8f30318 100644 --- a/packages/client/src/relewise.client.ts +++ b/packages/client/src/relewise.client.ts @@ -10,13 +10,13 @@ export interface RelewiseRequestOptions { } export class ProblemDetailsError extends Error { - private _details?: HttpProblemDetails; + private _details?: HttpProblemDetails | null; - public get details(): HttpProblemDetails | undefined { + public get details(): HttpProblemDetails | undefined | null { return this._details; } - constructor(message: string, details?: HttpProblemDetails) { + constructor(message: string, details?: HttpProblemDetails | null) { super(message); this._details = details; } @@ -66,18 +66,20 @@ export abstract class RelewiseClient { }); if (!response.ok) { - let responseMessage = null; + let responseMessage: HttpProblemDetails | null = null; + try { responseMessage = await response.json(); } catch (_) { } - 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); + const details = responseMessage?.detail ? `Details: ${responseMessage.detail}\n` : ''; + + 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); } try { const responseMessage = await response.json(); - return responseMessage as TResponse; } catch (err) { return undefined; diff --git a/packages/client/tests/integration-tests/tracker.integration.test.ts b/packages/client/tests/integration-tests/tracker.integration.test.ts index 440098b..7fcac59 100644 --- a/packages/client/tests/integration-tests/tracker.integration.test.ts +++ b/packages/client/tests/integration-tests/tracker.integration.test.ts @@ -172,7 +172,9 @@ test('Track Product View with invalid key', async() => { }).catch((e) => { expect(e).toBeDefined(); expect((e as ProblemDetailsError).details?.title).toEqual('Unauthorized'); - 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.') + expect(e.message).toContain('Error when calling the Relewise API.') + expect(e.message).toContain('Title: Unauthorized') + expect(e.message).toContain('Status: 401') }); });