Skip to content

Commit 1d2b400

Browse files
committed
store discovery errors
1 parent 4421cc6 commit 1d2b400

File tree

4 files changed

+74
-18
lines changed

4 files changed

+74
-18
lines changed

src/lib/httpClient.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import FormData from 'form-data';
33
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
4-
import { Env, Snapshot, ProcessedSnapshot, Git, Build, Context } from '../types.js';
4+
import { Env, Snapshot, ProcessedSnapshot, Git, Build, Context, DiscoveryErrors } from '../types.js';
55
import constants from './constants.js';
66
import type { Logger } from 'winston'
77
import pkgJSON from './../../package.json'
@@ -91,6 +91,9 @@ export default class httpClient {
9191
if (config && config.data && !config.data.name) {
9292
log.debug(config.data);
9393
}
94+
if (config && config.data && config.data.snapshotUuid) {
95+
log.debug(config.data);
96+
}
9497
return this.axiosInstance.request(config)
9598
.then(resp => {
9699
if (resp) {
@@ -222,7 +225,7 @@ export default class httpClient {
222225
}
223226

224227

225-
uploadSnapshot(ctx: Context, snapshot: ProcessedSnapshot) {
228+
uploadSnapshot(ctx: Context, snapshot: ProcessedSnapshot, discoveryErrors: DiscoveryErrors) {
226229
// Use capsBuildId if provided, otherwise fallback to ctx.build.id
227230
return this.request({
228231
url: `/builds/${ctx.build.id}/snapshot`,
@@ -233,12 +236,13 @@ export default class httpClient {
233236
test: {
234237
type: ctx.testType,
235238
source: 'cli'
236-
}
239+
},
240+
discoveryErrors: discoveryErrors,
237241
}
238242
}, ctx.log)
239243
}
240244

241-
processSnapshot(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string) {
245+
processSnapshot(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, discoveryErrors: DiscoveryErrors) {
242246
return this.request({
243247
url: `/build/${ctx.build.id}/snapshot`,
244248
method: 'POST',
@@ -252,11 +256,12 @@ export default class httpClient {
252256
source: 'cli'
253257
},
254258
async: false,
259+
discoveryErrors: discoveryErrors,
255260
}
256261
}, ctx.log)
257262
}
258263

259-
processSnapshotCaps(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, capsBuildId: string, capsProjectToken: string) {
264+
processSnapshotCaps(ctx: Context, snapshot: ProcessedSnapshot, snapshotUuid: string, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors) {
260265
return this.request({
261266
url: `/build/${capsBuildId}/snapshot`,
262267
method: 'POST',
@@ -273,11 +278,12 @@ export default class httpClient {
273278
source: 'cli'
274279
},
275280
async: false,
281+
discoveryErrors: discoveryErrors,
276282
}
277283
}, ctx.log)
278284
}
279285

280-
uploadSnapshotForCaps(ctx: Context, snapshot: ProcessedSnapshot, capsBuildId: string, capsProjectToken: string) {
286+
uploadSnapshotForCaps(ctx: Context, snapshot: ProcessedSnapshot, capsBuildId: string, capsProjectToken: string, discoveryErrors: DiscoveryErrors) {
281287
// Use capsBuildId if provided, otherwise fallback to ctx.build.id
282288
const buildId = capsBuildId !== '' ? capsBuildId : ctx.build.id;
283289

@@ -293,7 +299,8 @@ export default class httpClient {
293299
test: {
294300
type: ctx.testType,
295301
source: 'cli'
296-
}
302+
},
303+
discoveryErrors: discoveryErrors,
297304
}
298305
}, ctx.log);
299306
}

src/lib/processSnapshot.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Snapshot, Context, ProcessedSnapshot } from "../types.js";
1+
import { Snapshot, Context, DiscoveryErrors } from "../types.js";
22
import { scrollToBottomAndBackToTop, getRenderViewports, getRenderViewportsForOptions } from "./utils.js"
33
import { chromium, Locator } from "@playwright/test"
44
import constants from "./constants.js";
@@ -16,7 +16,17 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
1616
updateLogContext({ task: 'discovery' });
1717
ctx.log.debug(`Processing snapshot ${snapshot.name} ${snapshot.url}`);
1818
const isHeadless = process.env.HEADLESS?.toLowerCase() === 'false' ? false : true;
19+
let discoveryErrors: DiscoveryErrors = {
20+
name: "",
21+
url: "",
22+
timestamp: "",
23+
snapshotUUID: "",
24+
browsers: {
25+
chrome: {}
26+
}
27+
};
1928

29+
let globalViewport = ""
2030
let launchOptions: Record<string, any> = {
2131
headless: isHeadless,
2232
args: constants.LAUNCH_ARGS
@@ -84,7 +94,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
8494
...constants.REQUEST_HEADERS
8595
}
8696
}
87-
97+
8898
try {
8999
// abort audio/video media requests
90100
if (/\.(mp3|mp4|wav|ogg|webm)$/i.test(request.url())) {
@@ -141,10 +151,23 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
141151
ctx.log.debug(`Handling request ${requestUrl}\n - skipping already cached resource`);
142152
} else if (body.length > MAX_RESOURCE_SIZE) {
143153
ctx.log.debug(`Handling request ${requestUrl}\n - skipping resource larger than 15MB`);
144-
} else if (!ALLOWED_STATUSES.includes(response.status())) {
145-
ctx.log.debug(`Handling request ${requestUrl}\n - skipping disallowed status [${response.status()}]`);
146154
} else if (!ALLOWED_RESOURCES.includes(request.resourceType())) {
147155
ctx.log.debug(`Handling request ${requestUrl}\n - skipping disallowed resource type [${request.resourceType()}]`);
156+
} else if (!ALLOWED_STATUSES.includes(response.status())) {
157+
ctx.log.debug(`${globalViewport} Handling request ${requestUrl}\n - skipping disallowed status [${response.status()}]`);
158+
let data = {
159+
statusCode: `${response.status()}`,
160+
url: requestUrl,
161+
resourceType: request.resourceType(),
162+
}
163+
if (!discoveryErrors.browsers.chrome) {
164+
discoveryErrors.browsers.chrome = {};
165+
}
166+
if (!discoveryErrors.browsers.chrome[globalViewport]) {
167+
discoveryErrors.browsers.chrome[globalViewport] = [];
168+
}
169+
discoveryErrors.browsers.chrome[globalViewport]?.push(data);
170+
148171
} else {
149172
ctx.log.debug(`Handling request ${requestUrl}\n - content-type ${response.headers()['content-type']}`);
150173

@@ -304,10 +327,14 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
304327

305328
await page.setViewportSize({ width: viewport.width, height: viewport.height || MIN_VIEWPORT_HEIGHT });
306329
ctx.log.debug(`Page resized to ${viewport.width}x${viewport.height || MIN_VIEWPORT_HEIGHT}`);
330+
globalViewport = viewportString;
331+
ctx.log.debug(`globalViewport : ${globalViewport}`);
307332

308333
// navigate to snapshot url once
309334
if (!navigated) {
310335
try {
336+
discoveryErrors.url = snapshot.url;
337+
discoveryErrors.name = snapshot.name;
311338
// domcontentloaded event is more reliable than load event
312339
await page.goto(snapshot.url, { waitUntil: "domcontentloaded", timeout: ctx.config.waitForDiscovery });
313340
// adding extra timeout since domcontentloaded event is fired pretty quickly
@@ -399,6 +426,8 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
399426
ctx.log.debug(`Processed options: ${JSON.stringify(processedOptions)}`);
400427
}
401428

429+
discoveryErrors.timestamp = new Date().toISOString();
430+
402431
return {
403432
processedSnapshot: {
404433
name: snapshot.name,
@@ -407,6 +436,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
407436
resources: cache,
408437
options: processedOptions
409438
},
410-
warnings: [...optionWarnings, ...snapshot.dom.warnings]
439+
warnings: [...optionWarnings, ...snapshot.dom.warnings],
440+
discoveryErrors: discoveryErrors
411441
}
412442
}

src/lib/snapshotQueue.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ export default class Queue {
313313
}
314314

315315
// Process and upload snapshot
316-
let { processedSnapshot, warnings } = await processSnapshot(snapshot, this.ctx);
316+
let { processedSnapshot, warnings, discoveryErrors } = await processSnapshot(snapshot, this.ctx);
317317

318318
if (useCapsBuildId) {
319319
if (useKafkaFlowCaps) {
@@ -322,9 +322,9 @@ export default class Queue {
322322
const uploadUrl = presignedResponse.data.url;
323323

324324
await this.ctx.client.uploadSnapshotToS3Caps(this.ctx, uploadUrl, processedSnapshot, capsProjectToken)
325-
await this.ctx.client.processSnapshotCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken);
325+
await this.ctx.client.processSnapshotCaps(this.ctx, processedSnapshot, snapshotUuid, capsBuildId, capsProjectToken, discoveryErrors);
326326
} else {
327-
await this.ctx.client.uploadSnapshotForCaps(this.ctx, processedSnapshot, capsBuildId, capsProjectToken);
327+
await this.ctx.client.uploadSnapshotForCaps(this.ctx, processedSnapshot, capsBuildId, capsProjectToken, discoveryErrors);
328328
}
329329

330330
// Increment snapshot count for the specific buildId
@@ -358,9 +358,9 @@ export default class Queue {
358358
const uploadUrl = presignedResponse.data.url;
359359

360360
await this.ctx.client.uploadSnapshotToS3(this.ctx, uploadUrl, processedSnapshot)
361-
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid);
361+
await this.ctx.client.processSnapshot(this.ctx, processedSnapshot, snapshotUuid, discoveryErrors);
362362
} else {
363-
await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot);
363+
await this.ctx.client.uploadSnapshot(this.ctx, processedSnapshot, discoveryErrors);
364364
}
365365
this.ctx.totalSnapshots++;
366366
}

src/types.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,23 @@ export interface basicAuth {
186186
export interface FigmaWebConfig {
187187
autoDetectViewports: Array<string>;
188188
configs: Array<{ figma_file_token: string, figma_ids: Array<string>, screenshot_names:Array<string> }>;
189-
}
189+
}
190+
191+
192+
export interface ViewportErrors {
193+
statusCode: "aborted" | "404" | string;
194+
url: string;
195+
resourceType: string;
196+
}
197+
198+
export interface DiscoveryErrors {
199+
name: string;
200+
url: string;
201+
timestamp: string;
202+
snapshotUUID: string;
203+
browsers: {
204+
[browserName: string]: {
205+
[viewport: string]: ViewportErrors[];
206+
};
207+
};
208+
}

0 commit comments

Comments
 (0)