{"record":{"id":"28b7575c15d64afc","repo":"krahets/hello-algo","slug":"error-28b757","errorCode":null,"errorMessage":"キューが空です","messagePattern":"キューが空です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/typescript/chapter_stack_and_queue/array_queue.ts","lineNumber":58,"sourceCode":"        // 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする\n        const rear = (this.front + this.queSize) % this.capacity;\n        // num をキュー末尾に追加\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* デキュー */\n    pop(): number {\n        const num = this.peek();\n        // 先頭ポインタを1つ後ろへ進め、末尾を越えたら配列先頭に戻す\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* キュー先頭の要素にアクセス */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('キューが空です');\n        return this.nums[this.front];\n    }\n\n    /* Array を返す */\n    toArray(): number[] {\n        // 有効長の範囲内のリスト要素のみを変換\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n\n/* Driver Code */\n/* キューを初期化 */\nconst capacity = 10;\nconst queue = new ArrayQueue(capacity);","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/typescript/chapter_stack_and_queue/array_queue.ts#L40-L76","documentation":"Thrown by peek() on the array-backed queue when `isEmpty()` is true. Plain Error. pop() calls peek() internally first, so pop() on an empty queue propagates this exact throw before mutating front/queSize.","triggerScenarios":"Calling peek() or pop() (delegates to peek) when queSize is 0; consuming from a queue that has been fully drained; calling pop after a reset without re-checking size.","commonSituations":"BFS-style loops that pop one extra element; producer-consumer where the consumer outpaces the producer; assuming peek returns undefined on empty.","solutions":["Guard with `if (!queue.isEmpty())` or loop `while (!queue.isEmpty())`.","Catch the Error if empty is a recoverable condition.","Audit loop bounds so you never pop more than `queue.size()` times."],"exampleFix":"// before\nconst head = queue.peek();\n\n// after\nconst head = queue.isEmpty() ? undefined : queue.peek();","handlingStrategy":"validation","validationCode":"// Guard queue access.\nif (!queue.isEmpty()) {\n  const head = queue.peek();\n}","typeGuard":"function queueCanPeek(queue) {\n  return !queue.isEmpty();\n}","tryCatchPattern":"try {\n  const head = queue.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === 'キューが空です') {\n    // empty queue\n  } else throw e;\n}","preventionTips":["Check isEmpty() before peek/pop (pop delegates to peek).","Drain with `while (!queue.isEmpty())`.","In BFS loops, pop exactly size times.","Do not assume peek returns undefined on empty."],"tags":["queue","empty-state","typescript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}