in packages/synthetics-sdk-broken-links/src/navigation_func.ts [170:248]
export async function checkLink(
page: Page,
link: LinkIntermediate,
options: BrokenLinksResultV1_BrokenLinkCheckerOptions,
storageParams: StorageParameters,
isOrigin = false
): Promise<BrokenLinksResultV1_SyntheticLinkResult> {
// Determine the expected status code for the link, using per-link setting if
// available, else use default 2xx class
const expectedStatusCode: ResponseStatusCode = options.per_link_options[
link.target_uri
]?.expected_status_code ?? {
status_class: ResponseStatusCode_StatusClass.STATUS_CLASS_2XX,
};
// Perform the navigation and retrieves info to return
const {
responseOrError,
passed,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
retriesRemaining,
linkStartTime,
linkEndTime,
} = await navigate(page, link, options, expectedStatusCode);
let screenshotOutput: ApiScreenshotOutput = {
screenshot_file: '',
screenshot_error: {} as BaseError,
};
if (shouldTakeScreenshot(options, passed)) {
screenshotOutput = await uploadScreenshotToGCS(
page,
storageParams,
options
);
}
// Initialize variables for error information
let errorType = '';
let errorMessage = '';
if (responseOrError instanceof Error) {
errorType = responseOrError.name;
errorMessage = responseOrError.message;
} else if (!passed) {
// The link did not pass and no Puppeteer Error was thrown, manually set
// error information
errorType = 'BrokenLinksSynthetic_IncorrectStatusCode';
const classOrCode = expectedStatusCode.status_class ? 'class' : 'code';
const expectedStatus =
expectedStatusCode.status_class ?? expectedStatusCode.status_value;
errorMessage =
`${link?.target_uri} returned status code ` +
`${responseOrError?.status()} when a ${expectedStatus} status ` +
`${classOrCode} was expected.`;
}
const response = isHTTPResponse(responseOrError)
? (responseOrError as HTTPResponse)
: undefined;
return {
link_passed: passed,
expected_status_code: expectedStatusCode,
source_uri: options.origin_uri,
target_uri: link.target_uri,
html_element: link.html_element,
anchor_text: link.anchor_text,
status_code: response?.status(),
error_type: errorType,
error_message: errorMessage,
link_start_time: linkStartTime,
link_end_time: linkEndTime,
is_origin: isOrigin,
screenshot_output: screenshotOutput,
};
}