refactor: eliminate all catch(err: any) with proper unknown typing + type email transport
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m10s

- Replace all catch(err: any) with catch(err: unknown) across 8 source files
- Add errorMessage() and errorCode() helpers for safe error property access
- Type nodemailer transport config as SMTPTransport.Options (was any)
- Type health endpoint databaseStatus (was any)
- Type convert route margin param (was any)
- Change queryWithRetry params from any[] to unknown[]
- Update isTransientError to require Error instances (was accepting plain objects)
- 19 new TDD tests (error-type-safety.test.ts)
- Updated existing tests to use proper Error instances
- 598 tests total, all passing, zero type errors
This commit is contained in:
DocFast CEO 2026-03-09 11:10:58 +01:00
parent da049b77e3
commit 5a7ee79316
12 changed files with 221 additions and 98 deletions

View file

@ -3,6 +3,7 @@ import { renderPdf, renderUrlPdf } from "../services/browser.js";
import { markdownToHtml, wrapHtml } from "../services/markdown.js";
import dns from "node:dns/promises";
import logger from "../services/logger.js";
import { errorMessage } from "../utils/errors.js";
import { isPrivateIP } from "../utils/network.js";
import { sanitizeFilename } from "../utils/sanitize.js";
@ -122,13 +123,14 @@ convertRouter.post("/html", async (req: Request, res: Response) => {
res.setHeader("Content-Disposition", `inline; filename="${filename}"`);
res.setHeader("X-Render-Time", String(durationMs));
res.send(pdf);
} catch (err: any) {
} catch (err: unknown) {
logger.error({ err }, "Convert HTML error");
if (err.message === "QUEUE_FULL") {
const msg = errorMessage(err);
if (msg === "QUEUE_FULL") {
res.status(503).json({ error: "Server busy — too many concurrent PDF generations. Please try again in a few seconds." });
return;
}
if (err.message === "PDF_TIMEOUT") {
if (msg === "PDF_TIMEOUT") {
res.status(504).json({ error: "PDF generation timed out." });
return;
}
@ -228,13 +230,14 @@ convertRouter.post("/markdown", async (req: Request, res: Response) => {
res.setHeader("Content-Disposition", `inline; filename="${filename}"`);
res.setHeader("X-Render-Time", String(durationMs));
res.send(pdf);
} catch (err: any) {
} catch (err: unknown) {
logger.error({ err }, "Convert MD error");
if (err.message === "QUEUE_FULL") {
const msg = errorMessage(err);
if (msg === "QUEUE_FULL") {
res.status(503).json({ error: "Server busy — too many concurrent PDF generations. Please try again in a few seconds." });
return;
}
if (err.message === "PDF_TIMEOUT") {
if (msg === "PDF_TIMEOUT") {
res.status(504).json({ error: "PDF generation timed out." });
return;
}
@ -308,7 +311,7 @@ convertRouter.post("/url", async (req: Request, res: Response) => {
res.status(415).json({ error: "Unsupported Content-Type. Use application/json." });
return;
}
const body = req.body as { url?: string; format?: string; landscape?: boolean; margin?: any; printBackground?: boolean; waitUntil?: string; filename?: string; headerTemplate?: string; footerTemplate?: string; displayHeaderFooter?: boolean; scale?: number; pageRanges?: string; preferCSSPageSize?: boolean; width?: string; height?: string };
const body = req.body as { url?: string; format?: string; landscape?: boolean; margin?: string | { top?: string; right?: string; bottom?: string; left?: string }; printBackground?: boolean; waitUntil?: string; filename?: string; headerTemplate?: string; footerTemplate?: string; displayHeaderFooter?: boolean; scale?: number; pageRanges?: string; preferCSSPageSize?: boolean; width?: string; height?: string };
if (!body.url) {
res.status(400).json({ error: "Missing 'url' field" });
@ -365,13 +368,14 @@ convertRouter.post("/url", async (req: Request, res: Response) => {
res.setHeader("Content-Disposition", `inline; filename="${filename}"`);
res.setHeader("X-Render-Time", String(durationMs));
res.send(pdf);
} catch (err: any) {
} catch (err: unknown) {
logger.error({ err }, "Convert URL error");
if (err.message === "QUEUE_FULL") {
const msg = errorMessage(err);
if (msg === "QUEUE_FULL") {
res.status(503).json({ error: "Server busy — too many concurrent PDF generations. Please try again in a few seconds." });
return;
}
if (err.message === "PDF_TIMEOUT") {
if (msg === "PDF_TIMEOUT") {
res.status(504).json({ error: "PDF generation timed out." });
return;
}