{"record":{"id":"caac868d8f3b2ba2","repo":"Stirling-Tools/Stirling-PDF","slug":"failed-to-create-zip-file-error-instanceof-erro","errorCode":null,"errorMessage":"Failed to create ZIP file: ${error instanceof Error ? error.message : \"Unknown error\"}","messagePattern":"Failed to create ZIP file: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/editor/src/core/services/zipFileService.ts","lineNumber":176,"sourceCode":"        const content = await file.arrayBuffer();\n        zip.file(file.name, content);\n      }\n\n      // Generate ZIP blob\n      const zipBlob = await zip.generateAsync({\n        type: \"blob\",\n        compression: \"DEFLATE\",\n        compressionOptions: { level: 6 },\n      });\n\n      const zipFile = new File([zipBlob], zipFilename, {\n        type: \"application/zip\",\n        lastModified: Date.now(),\n      });\n\n      return { zipFile, size: zipFile.size };\n    } catch (error) {\n      throw new Error(\n        `Failed to create ZIP file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n        {\n          cause: error,\n        },\n      );\n    }\n  }\n\n  /**\n   * Extract PDF files from a ZIP archive\n   */\n  async extractPdfFiles(\n    file: File,\n    onProgress?: (progress: ZipExtractionProgress) => void,\n  ): Promise<ZipExtractionResult> {\n    const result: ZipExtractionResult = {\n      success: false,\n      extractedFiles: [],","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/services/zipFileService.ts#L158-L194","documentation":"Wraps any exception thrown inside createZipFromFiles: a File whose arrayBuffer() rejects (revoked blob), JSZip generateAsync failing (out of memory), or File construction failing. The original error is preserved as cause.","triggerScenarios":"A File/Blob passed in is no longer readable (blob URL revoked), the combined content exceeds available memory during DEFLATE compression, or a non-File value lacks arrayBuffer.","commonSituations":"Revoked object URLs; very large or many files exhausting the heap/WASM; Safari blob backing-store loss; concurrent zip operations competing for memory.","solutions":["Ensure all File objects are still valid (not revoked) at call time.","Reduce total input size or zip in smaller batches.","Catch the error, inspect e.cause, and surface it to the user; retry once for transient memory pressure.","Validate inputs are real File instances before calling."],"exampleFix":"// before\nconst content = await file.arrayBuffer();\nzip.file(file.name, content);\n// ...\nconst zipBlob = await zip.generateAsync({ type: 'blob', compression: 'DEFLATE' });\n\n// after\nif (!files.every((f) => f instanceof File && f.size >= 0)) {\n  throw new Error('All inputs must be valid File objects.');\n}\n// stream large sets in chunks instead of one giant generateAsync\nfor (const f of files) {\n  zip.file(f.name, await f.arrayBuffer());\n}","handlingStrategy":"try-catch","validationCode":"if (!files.every((f) => f instanceof File)) {\n  throw new Error('createZipFromFiles requires File instances.');\n}\nif (files.reduce((n, f) => n + f.size, 0) > MAX_ZIP_BYTES) {\n  throw new Error('Combined size too large to zip in memory.');\n}","typeGuard":"function areValidFiles(files: unknown[]): files is File[] {\n  return files.every((f) => f instanceof File && typeof f.arrayBuffer === 'function');\n}","tryCatchPattern":"try {\n  return await zipFileService.createZipFromFiles(files, name);\n} catch (e) {\n  const cause = (e instanceof Error && e.cause) ? e.cause : e;\n  console.error('ZIP creation failed:', cause);\n  throw e;\n}","preventionTips":["Do not revoke object URLs until the zip is fully generated.","Cap total input size and chunk very large batches.","Always read e.cause to distinguish revoked-blob vs memory errors."],"tags":["zip","memory","blob","jszip"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}