{"record":{"id":"cdb684d7d4d218ec","repo":"Stirling-Tools/Stirling-PDF","slug":"failed-to-process-file-name-error-instanceof","errorCode":null,"errorMessage":"Failed to process ${file.name}: ${error instanceof Error ? error.message : \"Unknown error\"}","messagePattern":"Failed to process (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/editor/src/core/hooks/tools/removeAnnotations/useRemoveAnnotationsOperation.ts","lineNumber":65,"sourceCode":"                err,\n              );\n            }\n          }\n\n          m.FPDF_ClosePage(pagePtr);\n        }\n\n        const outBytes = await saveRawDocument(docPtr);\n        const processedFile = new File([outBytes], file.name, {\n          type: \"application/pdf\",\n        });\n        processedFiles.push(processedFile);\n      } finally {\n        closeDocAndFreeBuffer(m, docPtr);\n      }\n    } catch (error) {\n      console.error(\"Error processing file:\", file.name, error);\n      throw new Error(\n        `Failed to process ${file.name}: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n        {\n          cause: error,\n        },\n      );\n    }\n  }\n\n  return {\n    files: processedFiles,\n    consumedAllInputs: false,\n  };\n};\n\n// Static configuration object\nexport const removeAnnotationsOperationConfig = defineCustomTool({\n  operationType: \"removeAnnotations\",\n  customProcessor: removeAnnotationsProcessor,","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/hooks/tools/removeAnnotations/useRemoveAnnotationsOperation.ts#L47-L83","documentation":"The outer catch-all in removeAnnotationsProcessor. It wraps any error thrown while opening the PDF with PDFium, removing annotations, or saving — rethrowing with the offending filename and the original error as `cause`. The inner message is whatever the PDFium/WASM layer produced (allocation failure, corrupt PDF, save error).","triggerScenarios":"openRawDocumentSafe fails on a corrupt or encrypted PDF; FPDF_LoadPage returns null and a downstream call dereferences it; saveRawDocument fails (WASM memory); the source file is not actually a PDF despite its extension.","commonSituations":"User drops an encrypted/password-protected PDF; a malformed PDF that PDFium rejects; very large PDF exhausting WASM memory during save; file got renamed to .pdf but is another format.","solutions":["Read `error.cause` to find the underlying PDFium failure and address that specifically (memory, encryption, corruption).","Validate the file is a readable, unencrypted PDF before entering the PDFium pipeline (check %PDF magic, attempt a probe open).","Handle encrypted PDFs explicitly with a user-facing 'This PDF is password-protected' message.","Cap input size and page count to avoid WASM exhaustion during save."],"exampleFix":"// before\nthrow new Error(`Failed to process ${file.name}: ${error instanceof Error ? error.message : \"Unknown error\"}`, { cause: error });\n\n// after — classify known causes for clearer UX\nconst reason = /encrypt|password/i.test(String(error))\n  ? \"PDF is password-protected\"\n  : error instanceof Error ? error.message : \"Unknown error\";\nthrow new Error(`Failed to process ${file.name}: ${reason}`, { cause: error });","handlingStrategy":"try-catch","validationCode":"// Validate the file is a readable, unencrypted PDF before PDFium processing\nconst head = new Uint8Array(await file.slice(0, 5).arrayBuffer());\nconst isPdfMagic = String.fromCharCode(...head) === \"%PDF-\";\nif (!isPdfMagic) {\n  // reject up front with a clear 'not a PDF' message\n}","typeGuard":null,"tryCatchPattern":"try {\n  await removeAnnotationsProcessor(params, files);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"Failed to process \") && e.cause) {\n    const cause = e.cause instanceof Error ? e.cause.message : String(e.cause);\n    if (/encrypt|password/i.test(cause)) toast.error(\"This PDF is password-protected.\");\n    else throw e;\n  } else throw e;\n}","preventionTips":["Check the %PDF magic and probe for encryption before entering the PDFium pipeline.","Cap input size/page count to avoid WASM exhaustion during save.","Read error.cause to classify and address the underlying PDFium failure."],"tags":["pdfium","remove-annotations","error-wrapping","wasm"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}