{"record":{"id":"912e1c30b2eed1eb","repo":"krahets/hello-algo","slug":"error-912e1c","errorCode":null,"errorMessage":"队列为空","messagePattern":"队列为空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"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        // 队首指针向后移动一位，若越过尾部，则返回到数组头部\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/codes/javascript/chapter_stack_and_queue/array_queue.js#L39-L75","documentation":"Thrown by peek() on an array-backed circular queue when the queue is empty (queSize === 0). pop() calls peek() first, so both throw identically. peek() returns #nums[#front]; on an empty queue that slot holds stale data, hence the guard.","triggerScenarios":"Calling peek()/pop() on a freshly constructed queue; dequeuing more items than were enqueued; drain loop without an emptiness check.","commonSituations":"BFS level processing; task queue drained faster than it is filled; mismatched enqueue/dequeue counts across async boundaries.","solutions":["Check queue.isEmpty() (size === 0) before peek/pop.","Drain with while (!q.isEmpty()) q.pop();.","Track enqueued count and never dequeue beyond it.","For producer/consumer flow, signal completion instead of relying on the throw."],"exampleFix":"// before\nconst head = q.peek(); // throws if empty\n// after\nconst head = q.isEmpty() ? null : q.peek();","handlingStrategy":"validation","validationCode":"function safePeek(q) {\n  return q.isEmpty() ? null : q.peek();\n}","typeGuard":"function queueNotEmpty(q) {\n  return q.size > 0;\n}","tryCatchPattern":"try {\n  const head = q.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === '队列为空') { /* handle empty */ } else throw e;\n}","preventionTips":["Guard peek()/pop() with q.isEmpty().","Drain with while (!q.isEmpty()) q.pop();.","Track enqueue count and never dequeue past it.","For async producer/consumer, signal completion explicitly."],"tags":["queue","empty-state","javascript","circular-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}