Skip to content

Commit

Permalink
Merge pull request #78 from Relewise/feat-improve-error-handling
Browse files Browse the repository at this point in the history
feat: improve error handling
  • Loading branch information
mzanoni authored Aug 15, 2024
2 parents d4a46a9 + 8194508 commit b22d0ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions packages/client/src/relewise.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -67,18 +67,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
});
});

Expand Down

0 comments on commit b22d0ef

Please sign in to comment.