{"record":{"id":"5b2898a203bab1ad","repo":"toon-format/toon","slug":"unexpected-endarray-event","errorCode":null,"errorMessage":"Unexpected endArray event","messagePattern":"Unexpected endArray event","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/toon/src/decode/event-builder.ts","lineNumber":121,"sourceCode":"          if (parent.currentKey === undefined) {\n            throw new Error('Array startArray event without preceding key')\n          }\n          setOwnProperty(parent.obj, parent.currentKey, arr)\n          parent.currentKey = undefined\n        }\n        else if (parent.type === 'array') {\n          parent.arr.push(arr)\n        }\n\n        stack.push({ type: 'array', arr })\n      }\n\n      break\n    }\n\n    case 'endArray': {\n      if (stack.length === 0) {\n        throw new Error('Unexpected endArray event')\n      }\n\n      const context = stack.pop()!\n      if (context.type !== 'array') {\n        throw new Error('Mismatched endArray event')\n      }\n\n      if (stack.length === 0) {\n        state.root = context.arr\n      }\n\n      break\n    }\n\n    case 'key': {\n      if (stack.length === 0) {\n        throw new Error('Key event outside of object context')\n      }","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/toon-format/toon/blob/604eac266e35166bed6f5b9e3bd586e9c60a9330/packages/toon/src/decode/event-builder.ts#L103-L139","documentation":"applyEvent received an endArray event while the stack is empty — no open array exists to close, so the stream is unbalanced.","triggerScenarios":"A stream with more endArray events than startArray events, or a leading endArray.","commonSituations":"Corrupted/truncated streams; double-emission of endArray by custom producers.","solutions":["Balance each endArray with a startArray","Fix the producer emitting surplus endArray events","Pre-validate start/end counts"],"exampleFix":"// before\n[{type:'endArray'}] // unbalanced\n// after\n[{type:'startArray'},{type:'endArray'}]","handlingStrategy":"validation","validationCode":"function hasNoExtraEndArray(events) {\n  let depth = 0\n  for (const ev of events) {\n    if (ev.type === 'startArray') depth++\n    if (ev.type === 'endArray') { depth--; if (depth < 0) return false }\n  }\n  return depth === 0\n}","typeGuard":null,"tryCatchPattern":"try {\n  const value = buildValueFromEvents(events)\n} catch (e) {\n  if (e.message === 'Unexpected endArray event') {\n    throw new Error('Unbalanced event stream: endArray without startArray')\n  }\n  throw e\n}","preventionTips":["Assert array depth > 0 in the emitter before emitting endArray","Balance-check generated streams in tests","Never truncate streams mid-array; regenerate from source"],"tags":["event-stream","decoder","toon"],"backgroundTag":"malformed-event-stream","analyzedSha":"604eac266e35166bed6f5b9e3bd586e9c60a9330","analyzedAt":"2026-08-31T11:09:25.043Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}