{"record":{"id":"d3c3652357f468fc","repo":"can1357/oh-my-pi","slug":"failed-to-encode-asar-archive-describeerror-err","errorCode":null,"errorMessage":"Failed to encode ASAR archive: ${describeError(error)}","messagePattern":"Failed to encode ASAR archive: (.+?)","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/asar.ts","lineNumber":478,"sourceCode":"\t\ttry {\n\t\t\toutput = new Uint8Array(archiveSize);\n\t\t} catch {\n\t\t\tthrow new ArchiveError(\"ASAR archive is too large to encode in memory\");\n\t\t}\n\t\twriteUInt32LE(output, 0, 4);\n\t\twriteUInt32LE(output, 4, headerSize);\n\t\twriteUInt32LE(output, 8, innerPayloadSize);\n\t\twriteUInt32LE(output, 12, jsonBytes.byteLength);\n\t\toutput.set(jsonBytes, ASAR_JSON_OFFSET);\n\t\tlet offset = dataOffset;\n\t\tfor (const payload of payloads) {\n\t\t\toutput.set(payload, offset);\n\t\t\toffset += payload.byteLength;\n\t\t}\n\t\treturn output;\n\t} catch (error) {\n\t\tif (error instanceof ArchiveError) throw error;\n\t\tthrow new ArchiveError(`Failed to encode ASAR archive: ${describeError(error)}`);\n\t}\n}\n","sourceCodeStart":460,"sourceCodeEnd":481,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/asar.ts#L460-L481","documentation":"encodeAsar wraps its whole encoding pipeline in a try/catch: any unexpected exception (JSON.stringify failure, TextEncoder issues, bugs in member handling) that is not already an ArchiveError is rethrown as ArchiveError(`Failed to encode ASAR archive: ${describeError(error)}`). ArchiveErrors thrown inside (size limits, path conflicts, allocation failures) pass through unchanged with their specific messages.","triggerScenarios":"Any non-ArchiveError exception inside encodeAsar — most commonly JSON.stringify throwing on circular structures if a custom root was constructed, or an underlying engine error during the output.set copies; i.e. anything that bypasses the pre-validated fast paths.","commonSituations":"Malformed member lists (non-Uint8Array payloads from an untyped call site), unexpected engine failures mid-copy on very large archives, or library version changes that altered internal invariants — surfaced here because the catch-all converts them into a uniform ArchiveError.","solutions":["Read the inner describeError message to identify the original failure cause.","Validate members before calling: each payload must be a Uint8Array and paths must be unique, non-conflicting strings.","If the message masks an allocation failure, treat it like the 'too large to encode in memory' case and shrink the payload.","Upgrade/report: if the underlying cause looks like a library bug, reproduce with the original error enabled by catching before this wrapper."],"exampleFix":"// before\nconst members = new Map([[\"a.txt\", \"not-a-uint8array\"]]); // string payload slips through untyped code\nencodeAsar(members); // -> Failed to encode ASAR archive: ...\n// after\nconst members = new Map([[\"a.txt\", new TextEncoder().encode(\"hello\")]]);\nconst payloads: Array<[string, Uint8Array]> = [...members].filter(([, v]) => v instanceof Uint8Array);\nencodeAsar(payloads);","handlingStrategy":"try-catch","validationCode":"function validateMembers(members) {\n  for (const [path, payload] of members) {\n    if (typeof path !== \"string\" || path.length === 0) throw new Error(`bad member path: ${String(path)}`);\n    if (!(payload instanceof Uint8Array)) throw new Error(`member '${path}' is not a Uint8Array`);\n  }\n}","typeGuard":"function isAsarMember(v) {\n  return Array.isArray(v) && typeof v[0] === \"string\" && v[1] instanceof Uint8Array;\n}","tryCatchPattern":"try {\n  return encodeAsar(members);\n} catch (err) {\n  if (err instanceof ArchiveError) {\n    if (err.message.startsWith(\"Failed to encode ASAR archive\")) {\n      logger.error(\"asar encode failed\", { cause: err.message });\n    }\n    throw err;\n  }\n  throw err;\n}","preventionTips":["Always pass Uint8Array payloads — validate the member list shape before calling.","Ensure member paths are unique and do not conflict (file vs directory).","Keep the underlying error visible: log err.message which embeds describeError.","Test packaging with production-sized inputs, not just fixtures."],"tags":["asar","archive","encoding","wrapper-error"],"backgroundTag":"archive-encode-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}