Skip to content

Commit

Permalink
fix(cdp): avoid throwing errors for unknown session IDs (#241)
Browse files Browse the repository at this point in the history
closes #173
  • Loading branch information
derevnjuk authored Mar 14, 2023
1 parent a3e474c commit 111981b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
12 changes: 4 additions & 8 deletions src/cdp/DefaultNetwork.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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' }])(
Expand Down
16 changes: 12 additions & 4 deletions src/cdp/DefaultNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
};

Expand All @@ -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') ||
Expand Down
9 changes: 8 additions & 1 deletion src/cdp/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 111981b

Please sign in to comment.