Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Aug 20, 2024
1 parent 355ee08 commit 2ca14db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/url-inspect/url-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,30 @@ export async function processUrlInspect(baseURL, context, pages) {
log.info(`Successfully inspected URL: ${page}`);

const filteredIndexStatusResult = {
verdict: inspectionResult.indexStatusResult?.verdict,
lastCrawlTime: inspectionResult.indexStatusResult?.lastCrawlTime,
verdict: inspectionResult?.indexStatusResult?.verdict,
lastCrawlTime: inspectionResult?.indexStatusResult?.lastCrawlTime,
};

const filteredRichResults = inspectionResult.richResultsResult?.detectedItems?.map(
const filteredRichResults = inspectionResult?.richResultsResult?.detectedItems?.map(
(item) => {
const filteredItems = item.items.filter(
(issueItem) => issueItem.issues.some(
(issue) => issue.severity === 'ERROR',
const filteredItems = item?.items?.filter(
(issueItem) => issueItem?.issues?.some(
(issue) => issue?.severity === 'ERROR',
),
).map((issueItem) => ({
name: issueItem.name,
issues: issueItem.issues.filter((issue) => issue.severity === 'ERROR'),
)?.map((issueItem) => ({
name: issueItem?.name,
issues: issueItem?.issues?.filter((issue) => issue?.severity === 'ERROR'),
}));

return {
richResultType: item.richResultType,
richResultType: item?.richResultType,
items: filteredItems,
};
},
).filter((item) => item.items.length > 0);
)?.filter((item) => item.items.length > 0) ?? [];

if (filteredRichResults.length > 0) {
filteredRichResults.verdict = inspectionResult.richResultsResult?.verdict;
filteredRichResults.verdict = inspectionResult?.richResultsResult?.verdict;
log.info(`Found ${filteredRichResults.length} rich results issues for URL: ${page}`);
} else {
log.info(`No rich results issues found for URL: ${page}`);
Expand Down
13 changes: 12 additions & 1 deletion test/audits/pdp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('URLInspect Audit', () => {

const auditData = await gscPdpStructuredDataHandler('https://www.example.com', context, siteStub);

expect(auditData.auditResult[0].richResults).to.equal(undefined);
expect(auditData.auditResult[0].richResults).to.deep.equal([]);
});

it('returns no rich results when there are no errors in rich results', async () => {
Expand Down Expand Up @@ -279,4 +279,15 @@ describe('URLInspect Audit', () => {
expect(error.message).to.equal('Failed to create Google client. Site was probably not onboarded to GSC yet. Error: Sinon-provided No secrets found');
}
});

it('throws error if google client fails to inspect URL', async () => {
urlInspectStub.rejects(new Error('Failed to inspect URL'));
sandbox.stub(GoogleClient, 'createFrom').returns(googleClientStub);

try {
await gscPdpStructuredDataHandler('https://www.example.com', context, siteStub);
} catch (error) {
expect(error.message).to.equal('Failed to inspect URL: https://example.com/product/1. Error: Failed to inspect URL');
}
});
});

0 comments on commit 2ca14db

Please sign in to comment.