{"record":{"id":"e94bc4d5872451ab","repo":"krahets/hello-algo","slug":"queue-is-empty","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/javascript/chapter_stack_and_queue/array_queue.js","lineNumber":57,"sourceCode":"        // Add num to the rear of the queue\n        const rear = (this.#front + this.size) % this.capacity;\n        // Front pointer moves one position backward\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* Dequeue */\n    pop() {\n        const num = this.peek();\n        // Move front pointer backward by one position, if it passes the tail, return to array head\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (this.isEmpty()) throw new Error('Queue is empty');\n        return this.#nums[this.#front];\n    }\n\n    /* Return Array */\n    toArray() {\n        // Elements enqueue\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/* Access front of the queue element */\nconst capacity = 10;\nconst queue = new ArrayQueue(capacity);","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/javascript/chapter_stack_and_queue/array_queue.js#L39-L75","documentation":"Thrown by peek() on an array-backed circular queue when empty. peek() returns this.#nums[this.#front]; pop() calls peek() first so pop also surfaces this error. The guard prevents returning undefined from an empty slot and keeps the front pointer arithmetic honest.","triggerScenarios":"Calling queue.pop() or queue.peek() on an empty queue; dequeuing more than was enqueued; FIFO processing where the consumer outpaces the producer.","commonSituations":"BFS frontier drained; message/buffer queue consumed faster than filled; loop that pops until falsy (but throw interrupts before undefined).","solutions":["Check queue.isEmpty() (or queue.size === 0) before pop/peek.","Use while (!queue.isEmpty()) { const v = queue.pop(); ... } for draining.","Track enqueue count to bound a fixed number of pops.","If pop must be defensive, catch the error and treat as end-of-stream."],"exampleFix":"// before\nwhile (true) { const v = queue.pop(); ... } // throws when empty\n\n// after\nwhile (!queue.isEmpty()) { const v = queue.pop(); ... }","handlingStrategy":"validation","validationCode":"if (!queue.isEmpty()) {\n  const v = queue.pop();\n} else {\n  // handle empty queue\n}","typeGuard":"function queueHasElements(queue) {\n  return typeof queue.isEmpty === 'function' && !queue.isEmpty();\n}","tryCatchPattern":"try {\n  const v = queue.pop();\n} catch (e) {\n  if (e.message === 'Queue is empty') { /* drained */ }\n  else throw e;\n}","preventionTips":["Check isEmpty() before pop/peek.","Drain with while (!queue.isEmpty()) pop().","Track enqueue count to bound a fixed number of pops."],"tags":["queue","circular-array","empty-state","javascript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}