Skip to content

Commit

Permalink
feat: better logs during timeout (#1208)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Moreau <[email protected]>
  • Loading branch information
realSpok and Nicolas Moreau authored Nov 13, 2024
1 parent d0fd424 commit e844184
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/forestadmin-client/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export default class ServerUtils {
body?: string | object,
maxTimeAllowed = 10000, // Set a default value if not provided
): Promise<T> {
try {
const url = new URL(path, options.forestServerUrl).toString();
let url;

try {
url = new URL(path, options.forestServerUrl).toString();
const request = superagent[method](url).timeout(maxTimeAllowed);

request.set('forest-secret-key', options.envSecret);
Expand All @@ -28,7 +29,11 @@ export default class ServerUtils {
return response.body;
} catch (error) {
if (error.timeout) {
throw new Error('The request to Forest Admin server has timeout');
throw new Error(
`The request to Forest Admin server has timed out while trying to reach ${url} at ${new Date().toISOString()}. Message: ${
error.message
}`,
);
}

this.handleResponseError(error);
Expand Down
7 changes: 6 additions & 1 deletion packages/forestadmin-client/test/utils/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ describe('ServerUtils', () => {
});

it('should timeout if the server take more than maxTimeAllowed to respond', async () => {
const mockDate = new Date(1466424490000);
const spy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate);
nock(options.forestServerUrl, {
reqheaders: { 'x-foo': 'bar', 'forest-secret-key': options.envSecret },
})
Expand All @@ -123,7 +125,10 @@ describe('ServerUtils', () => {
'',
100, // maxTimeAllowed to respond
),
).rejects.toThrow('The request to Forest Admin server has timeout');
).rejects.toThrow(
'The request to Forest Admin server has timed out while trying to reach http://forestadmin-server.com/endpoint at 2016-06-20T12:08:10.000Z. Message: Timeout of 100ms exceeded',
);
spy.mockRestore();
});

describe('when the server send back a message', () => {
Expand Down

0 comments on commit e844184

Please sign in to comment.