{"record":{"id":"4bd6f98cc5b498fe","repo":"mozilla/pdf.js","slug":"invalid-pageindex-request","errorCode":null,"errorMessage":"Invalid pageIndex request.","messagePattern":"Invalid pageIndex request\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/display/api.js","lineNumber":3059,"sourceCode":"        }\n\n        const page = new PDFPageProxy(\n          pageIndex,\n          pageInfo,\n          this,\n          this.pagesMapper,\n          this._params.pdfBug\n        );\n        this.#pageCache.set(pageIndex, page);\n        return page;\n      });\n    this.#pagePromises.set(pageIndex, promise);\n    return promise;\n  }\n\n  async getPageIndex(ref) {\n    if (!isRefProxy(ref)) {\n      throw new Error(\"Invalid pageIndex request.\");\n    }\n    const index = await this.messageHandler.sendWithPromise(\"GetPageIndex\", {\n      num: ref.num,\n      gen: ref.gen,\n    });\n    const pageNumber = this.pagesMapper.getPageNumber(index + 1);\n    if (pageNumber === 0) {\n      throw new Error(\"GetPageIndex: page has been removed.\");\n    }\n    return pageNumber - 1;\n  }\n\n  getAnnotations(pageIndex, intent) {\n    return this.messageHandler.sendWithPromise(\"GetAnnotations\", {\n      pageIndex: this.pagesMapper.getPageId(pageIndex + 1) - 1,\n      intent,\n    });\n  }","sourceCodeStart":3041,"sourceCodeEnd":3077,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/display/api.js#L3041-L3077","documentation":"Thrown by PDFDocumentProxy.getPageIndex(ref) when the supplied reference does not look like a PDF object reference. The guard isRefProxy requires an object with integer properties num>=0 and gen>=0 (matching an indirect object identifier). It exists because the worker message GetPageIndex needs a real reference; anything else cannot be resolved to a page.","triggerScenarios":"Calling pdfDocument.getPageIndex(ref) with undefined, null, a plain number, a string, or an object missing/overflowing num or gen. Common when chaining from getPage(dest[0]) where dest is an array that does not start with a ref, or when passing a deserialized ref that lost its fields.","commonSituations":"Resolving a named/remote GoTo destination whose target array begins with a page number instead of a ref; passing an outline destination straight through without validating; ref objects built from JSON without Number-casting their fields.","solutions":["Validate the value with the same shape as isRefProxy before calling: object with integer num>=0 and gen>=0.","If the source is a destination array, check Array.isArray(dest) && typeof dest[0] === 'object' before calling getPageIndex(dest[0]).","Use pdfDocument.getPageIndex only after pdfDocument.getPage has already resolved a ref, or cache refs from getPage output."],"exampleFix":"// before\nconst idx = await pdfDocument.getPageIndex(dest[0]);\n\n// after\nconst ref = dest?.[0];\nif (ref && Number.isInteger(ref.num) && ref.num >= 0 && Number.isInteger(ref.gen) && ref.gen >= 0) {\n  const idx = await pdfDocument.getPageIndex(ref);\n} else {\n  // handle string/null destination via getDestination\n}","handlingStrategy":"type-guard","validationCode":"function isRefLike(v) {\n  return v != null &&\n    typeof v === 'object' &&\n    Number.isInteger(v.num) && v.num >= 0 &&\n    Number.isInteger(v.gen) && v.gen >= 0;\n}\n// before calling:\nif (!isRefLike(ref)) return null;","typeGuard":"const isRefLike = (v): v is { num: number; gen: number } =>\n  !!v && typeof v === 'object' &&\n  Number.isInteger((v as any).num) && (v as any).num >= 0 &&\n  Number.isInteger((v as any).gen) && (v as any).gen >= 0;","tryCatchPattern":"try {\n  const idx = await pdf.getPageIndex(ref);\n} catch (e) {\n  if (e.message === 'Invalid pageIndex request.') { /* skip bad dest */ }\n  else throw e;\n}","preventionTips":["Only pass refs obtained from PDFPageProxy or resolved destinations.","Validate ref shape at the boundary where destinations enter your code.","Keep refs fully typed as { num: number; gen: number } in your codebase."],"tags":["api-misuse","validation","destinations"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}