Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error handling #78

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
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
Loading