Skip to content

Commit b1ef3f7

Browse files
SCAL-255099 added utility to evaluate response status correctly
1 parent 9408e19 commit b1ef3f7

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

src/utils.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -742,23 +742,12 @@ describe('checkInternetConnection', () => {
742742
});
743743

744744
it('should return true when internet connection is available', async () => {
745-
jest.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve({
746-
ok: true,
747-
status: 200,
748-
statusText: 'OK',
749-
}));
750745
const result = await checkInternetConnection(maxRetries , baseDelay , thoughtSpotHost);
751746
expect(result).toBe(true);
752747
expect(fetchSpy).toHaveBeenCalledTimes(1);
753748
});
754749

755750
it('should return false when internet connection is not available', async () => {
756-
jest.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve({
757-
ok: false,
758-
status: 404,
759-
statusText: 'Not Found',
760-
}));
761-
762751
const result = await checkInternetConnection(maxRetries , baseDelay , thoughtSpotHost);
763752
expect(result).toBe(false);
764753
expect(fetchSpy).toHaveBeenCalledTimes(maxRetries + 1);

src/utils.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from './types';
1919
import { logger } from './utils/logger';
2020
import { EndPoints } from './utils/authService';
21+
import { isActiveService } from './utils/authService/tokenizedAuthService';
2122

2223
/**
2324
* Construct a runtime filters query string from the given filters.
@@ -526,26 +527,16 @@ export const checkInternetConnection = async (
526527

527528
for (let attempt = 0; attempt <= maxRetries; attempt++) {
528529
try {
529-
const controller = new AbortController();
530-
const timeoutId = setTimeout(() => controller.abort(), 5000);
531-
532-
const response = await fetch(pingUrl, {
533-
method: 'HEAD',
534-
cache: 'no-store',
535-
signal: controller.signal,
536-
});
537-
538-
clearTimeout(timeoutId);
539-
540-
if (response.status >= 200 && response.status < 300) {
530+
const response = await isActiveService(thoughtSpotHost);
531+
logger.debug(`Pinging ${pingUrl} failed with response ${response}`);
532+
if (response) {
541533
if (attempt > 0) {
542534
logger.info('Internet connection restored');
543535
}
544536
return true;
545537
}
546-
547-
logger.debug(`Pinging ${pingUrl} failed with HTTP ${response.status}: ${response.statusText}`);
548-
throw new Error(`Server responded with status ${response.status}. Error: ${response.statusText}`);
538+
logger.debug(`Pinging ${pingUrl} failed with response ${response}`);
539+
throw new Error(`Server responded with status ${response}.`);
549540
} catch (error) {
550541
const isLastAttempt = attempt === maxRetries;
551542
const delayMs = baseDelay * Math.pow(2, attempt);

0 commit comments

Comments
 (0)