Skip to content

Commit a13a401

Browse files
authored
[patch] error handling (#99)
1 parent f32e5d2 commit a13a401

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/plugins/allure-reporter-plugin.ts

+40-25
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ const log = (...args: unknown[]) => {
4848
};
4949

5050
const createNewContentForContainer = (nameAttAhc: string, existingContents: Buffer, ext: string, specname: string) => {
51-
const containerJSON = JSON.parse(existingContents.toString());
51+
const getContentJson = () => {
52+
try {
53+
return JSON.parse(existingContents.toString());
54+
} catch (e) {
55+
console.error(`${packageLog} Could not parse the contents of attachment ${nameAttAhc}`);
56+
57+
return {};
58+
}
59+
};
60+
61+
const containerJSON = getContentJson();
5262

5363
const after: ExecutableItem = {
5464
name: 'video',
@@ -421,30 +431,35 @@ export class AllureReporter {
421431

422432
uuids.forEach(uuid => {
423433
const testFile = `${this.allureResults}/${uuid}-result.json`;
424-
const contents = readFileSync(testFile);
425-
const ext = path.extname(x.path);
426-
const name = path.basename(x.path);
427-
type ParsedAttachment = { name: string; type: ContentType; source: string };
428-
const testCon: { attachments: ParsedAttachment[] } = JSON.parse(contents.toString());
429-
const uuidNew = randomUUID();
430-
const nameAttAhc = `${uuidNew}-attachment${ext}`; // todo not copy same image
431-
const newPath = path.join(this.allureResults, nameAttAhc);
432-
433-
if (!existsSync(newPath)) {
434-
copyFileSync(x.path, path.join(this.allureResults, nameAttAhc));
435-
}
436434

437-
if (!testCon.attachments) {
438-
testCon.attachments = [];
439-
}
435+
try {
436+
const contents = readFileSync(testFile);
437+
const ext = path.extname(x.path);
438+
const name = path.basename(x.path);
439+
type ParsedAttachment = { name: string; type: ContentType; source: string };
440+
const testCon: { attachments: ParsedAttachment[] } = JSON.parse(contents.toString());
441+
const uuidNew = randomUUID();
442+
const nameAttAhc = `${uuidNew}-attachment${ext}`; // todo not copy same image
443+
const newPath = path.join(this.allureResults, nameAttAhc);
440444

441-
testCon.attachments.push({
442-
name: name,
443-
type: 'image/png',
444-
source: nameAttAhc, // todo
445-
});
445+
if (!existsSync(newPath)) {
446+
copyFileSync(x.path, path.join(this.allureResults, nameAttAhc));
447+
}
448+
449+
if (!testCon.attachments) {
450+
testCon.attachments = [];
451+
}
446452

447-
writeFileSync(testFile, JSON.stringify(testCon));
453+
testCon.attachments.push({
454+
name: name,
455+
type: 'image/png',
456+
source: nameAttAhc, // todo
457+
});
458+
459+
writeFileSync(testFile, JSON.stringify(testCon));
460+
} catch (e) {
461+
console.log(`Could not attach screenshot ${x.screenshotId}`);
462+
}
448463
});
449464
});
450465
}
@@ -474,7 +489,7 @@ export class AllureReporter {
474489
}
475490

476491
if (!existsSync(file)) {
477-
console.log(`file ${file} doesnt exist`);
492+
console.log(`${packageLog} file ${file} doesnt exist`);
478493

479494
return;
480495
}
@@ -515,14 +530,14 @@ export class AllureReporter {
515530

516531
readFile(videoPath, errVideo => {
517532
if (errVideo) {
518-
console.error(`Could not read video: ${errVideo}`);
533+
console.error(`${packageLog} Could not read video: ${errVideo}`);
519534

520535
return;
521536
}
522537

523538
testsAttach.forEach(test => {
524539
if (!test.parent) {
525-
console.error(`not writing videos since test has no parent suite: ${test.fullName}`);
540+
console.error(`${packageLog} not writing videos since test has no parent suite: ${test.fullName}`);
526541

527542
return;
528543
}

0 commit comments

Comments
 (0)