From 111981b5e1ce4fc199cf8531e8f8bc0ca339590b Mon Sep 17 00:00:00 2001 From: Artem Derevnjuk Date: Tue, 14 Mar 2023 18:40:08 +0400 Subject: [PATCH] fix(cdp): avoid throwing errors for unknown session IDs (#241) closes #173 --- src/cdp/DefaultNetwork.spec.ts | 12 ++++-------- src/cdp/DefaultNetwork.ts | 16 ++++++++++++---- src/cdp/messages.ts | 9 ++++++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/cdp/DefaultNetwork.spec.ts b/src/cdp/DefaultNetwork.spec.ts index 0887a61..cceb9fe 100644 --- a/src/cdp/DefaultNetwork.spec.ts +++ b/src/cdp/DefaultNetwork.spec.ts @@ -1,9 +1,6 @@ import { DefaultNetwork } from './DefaultNetwork'; import type { Logger } from '../utils/Logger'; -import { - TARGET_OR_BROWSER_CLOSED, - UNABLE_TO_ATTACH_TO_TARGET -} from './messages'; +import { TARGET_OR_BROWSER_CLOSED } from './messages'; import { anyFunction, anything, @@ -296,7 +293,7 @@ describe('DefaultNetwork', () => { ).once(); }); - it('should throw an error when an unexpected error is happened', async () => { + it('should not throw an error when an unexpected error is happened', async () => { // arrange const sessionId = '1'; const targetInfo: Protocol.Target.TargetInfo = { @@ -320,10 +317,9 @@ describe('DefaultNetwork', () => { ).thenReject(new Error('Something went wrong')); await sut.attachToTargets(listener); // act - const result = act?.({ sessionId, targetInfo, waitingForDebugger: true }); + await act?.({ sessionId, targetInfo, waitingForDebugger: true }); // assert - await expect(result).rejects.toThrow(); - verify(loggerMock.err(UNABLE_TO_ATTACH_TO_TARGET)).once(); + verify(loggerMock.warn(match('Unable to attach to the target'))).once(); }); it.each([{ input: 'Target closed' }, { input: 'Session closed' }])( diff --git a/src/cdp/DefaultNetwork.ts b/src/cdp/DefaultNetwork.ts index 03acdcd..38a8b6a 100644 --- a/src/cdp/DefaultNetwork.ts +++ b/src/cdp/DefaultNetwork.ts @@ -183,9 +183,17 @@ export class DefaultNetwork implements Network { ); } } catch (e) { - this.logger.err(UNABLE_TO_ATTACH_TO_TARGET); - this.logger.err(e); - throw e; + const stack = ErrorUtils.isError(e) ? e.stack : e; + + this.logger.debug( + `We encountered an issue while initializing the target for the session: ${sessionId}.` + ); + this.logger.debug( + `The information about the target is as follows: ${JSON.stringify( + targetInfo + )}` + ); + this.logger.warn(`${UNABLE_TO_ATTACH_TO_TARGET}\n${stack}`); } }; @@ -212,7 +220,7 @@ export class DefaultNetwork implements Network { } } - private targetClosedError(e: unknown): boolean { + private targetClosedError(e: unknown): e is Error { return ( ErrorUtils.isError(e) && (e.message.includes('Target closed') || diff --git a/src/cdp/messages.ts b/src/cdp/messages.ts index 58ed5a6..17f1cbc 100644 --- a/src/cdp/messages.ts +++ b/src/cdp/messages.ts @@ -7,7 +7,14 @@ export const DISCONNECTED = 'Chrome Debugging Protocol disconnected'; export const CONNECTION_NOT_ESTABLISHED = `Chrome Debugging Protocol connection has not been established.`; export const TARGET_OR_BROWSER_CLOSED = 'The target or browser may have closed before completion of initialization'; -export const UNABLE_TO_ATTACH_TO_TARGET = `Unable to attach to target. Please open an issue on the repository: https://github.com/NeuraLegion/cypress-har-generator/issues for assistance. +export const UNABLE_TO_ATTACH_TO_TARGET = `Unable to attach to the target (e.g. page, worker, etc). + +Possible reasons for the failure include: + - Chrome not running in headless mode. + - The target may have closed during initialization. + - The target may have crashed due to memory issues. + +Please open an issue on the repository: https://github.com/NeuraLegion/cypress-har-generator/issues for assistance. The stack trace for this error is:`; export const FAILED_TO_CONNECT = `${FAILED_ATTEMPT_TO_CONNECT}