Skip to content

Commit

Permalink
enhance error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj committed Sep 27, 2024
1 parent 46d9191 commit 90d8fad
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,11 @@ export class DocumentGenerator implements BaseTool<DocumentParameter> {
}

private static writeToFile(content: string | Buffer, filePath: string): void {
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
if (typeof content === "string") {
fs.writeFileSync(filePath, content, "utf8");
} else {
fs.writeFileSync(filePath, content);
}
} catch (error) {
throw error;
fs.mkdirSync(path.dirname(filePath), { recursive: true });
if (typeof content === "string") {
fs.writeFileSync(filePath, content, "utf8");
} else {
fs.writeFileSync(filePath, content);
}
}

Expand All @@ -187,10 +183,16 @@ export class DocumentGenerator implements BaseTool<DocumentParameter> {
private static async generatePdf(htmlContent: string): Promise<Buffer> {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent, { waitUntil: "networkidle0" });
const pdf = await page.pdf({ format: "A4" });
await browser.close();
return pdf;
try {
await page.setContent(htmlContent, { waitUntil: "networkidle0" });
const pdf = await page.pdf({ format: "A4" });
return pdf;
} catch (error) {
console.error("Error generating PDF:", error);
throw new Error("Failed to generate PDF");
} finally {
await browser.close();
}
}

async call(input: DocumentParameter): Promise<string> {
Expand Down

0 comments on commit 90d8fad

Please sign in to comment.