diff --git a/.gitignore b/.gitignore index ebf862613356377ccf22e58e8a08eef8c0cb051f..26dcb19dc5ea3d5e1949c31a7c9b7f57da768f07 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules .idea yarn-error.log __tests__/outputs/** +.DS_Store diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 6f25738f75c9b6f1bfc7b799a90e5f7bf8d69b42..19f6e5582b5557b5e46f526f181186a6dae06c7e 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -2,7 +2,7 @@ import * as fs from "fs"; import * as path from "path"; import { describe, it, expect } from "@jest/globals"; import PDFparser from "../src/pdfParser"; -import { AppError } from "../src/lib/errors"; +import { AppError, GeneralError } from "../src/lib/errors"; describe("PDF parser", () => { it("should return pdf document metadata including signatures", async () => { @@ -49,4 +49,17 @@ describe("PDF parser", () => { expect(error.message).toEqual("Only pdf file type is supported"); } }); + + it("should throw error when parsing file with unsupported subfilter type", async () => { + const file = fs.readFileSync( + path.resolve(__dirname, "./source/sample07.pdf") + ); + + try { + const parser = new PDFparser(file); + await parser.getPDFMeta(); + } catch (error) { + expect(error).toBeInstanceOf(GeneralError); + } + }); }); diff --git a/__tests__/source/sample07.pdf b/__tests__/source/sample07.pdf new file mode 100644 index 0000000000000000000000000000000000000000..6bf27ee7f55dbf39e1d730822f65fa4c5b71d766 Binary files /dev/null and b/__tests__/source/sample07.pdf differ diff --git a/src/lib/generalUtils.ts b/src/lib/generalUtils.ts index 7c749b5ba8d77aa08c45a1f9ef1d4384290ca79c..48509ab7fd383b357a5398891a97e5af6acc82f8 100644 --- a/src/lib/generalUtils.ts +++ b/src/lib/generalUtils.ts @@ -5,6 +5,7 @@ const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********"; export const checkForSubFilter = (pdfBuffer: Buffer) => { const matches = pdfBuffer.toString().match(/\/SubFilter\s*\/([\w.]*)/); const subFilter = Array.isArray(matches) && matches[1]; + if (!subFilter) { throw new AppError("Can not find subfilter"); } @@ -16,7 +17,9 @@ export const checkForSubFilter = (pdfBuffer: Buffer) => { ]; if (!supportedTypes.includes(subFilter.trim().toLowerCase())) { - throw new AppError("Subfilter not supported"); + throw new AppError( + `Subfilter not supported - ${subFilter.trim().toLowerCase()}` + ); } }; diff --git a/src/lib/index.ts b/src/lib/index.ts index 8e54bf946107e814ee583ebd800a75bb2efaa65b..81156ab550000e47034e5eece5da39106d147411 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -7,8 +7,7 @@ export const verifyPDF = (pdf: Buffer) => { try { checkForSubFilter(pdfBuffer); } catch (error) { - console.log("no supported signatures found"); - return null; + throw new Error(error.message); } try {