{"record":{"id":"e6c74ac9fd4524c2","repo":"krahets/hello-algo","slug":"error-e6c74a","errorCode":null,"errorMessage":"キューが空です","messagePattern":"キューが空です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/javascript/chapter_stack_and_queue/array_queue.js","lineNumber":57,"sourceCode":"        // 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする\n        const rear = (this.#front + this.size) % this.capacity;\n        // num をキュー末尾に追加\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* デキュー */\n    pop() {\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() {\n        if (this.isEmpty()) throw new Error('キューが空です');\n        return this.#nums[this.#front];\n    }\n\n    /* Array を返す */\n    toArray() {\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":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/javascript/chapter_stack_and_queue/array_queue.js#L39-L75","documentation":"Thrown by the array-based circular queue's peek when the queue is empty. peek returns the element at the front pointer; the guard prevents reading stale data when queSize is 0. pop() calls peek() first, so dequeue on an empty queue surfaces this same error.","triggerScenarios":"Calling peek() or pop() on an empty queue; dequeuing more items than were enqueued; calling at startup before any enqueue.","commonSituations":"BFS/level-order traversal where the queue legitimately drains; producer-consumer mismatch; calling pop in a loop with a wrong termination condition.","solutions":["Check queue.isEmpty() (or queue.size() === 0) before peek()/pop().","Use while (!queue.isEmpty()) for processing loops.","Return a sentinel when empty if a missing element is not an error.","Validate the enqueue/dequeue pairing in your logic."],"exampleFix":"// before\nconst head = queue.peek();  // throws if empty\n\n// after\nconst head = queue.isEmpty() ? null : queue.peek();","handlingStrategy":"validation","validationCode":"function safePeek(queue) {\n  return queue.isEmpty() ? null : queue.peek();\n}","typeGuard":"const isNonEmpty = (q) => typeof q.isEmpty === 'function' && !q.isEmpty();","tryCatchPattern":"try {\n  return queue.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === 'キューが空です') return null;\n  throw e;\n}","preventionTips":["Check isEmpty()/size() before peek()/pop().","Drain with while (!queue.isEmpty()).","Validate enqueue/dequeue pairing."],"tags":["queue","javascript","empty-state","circular-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}