{"record":{"id":"ca9012f0d49fa858","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-stroke","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.strokeRect","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.strokeRect","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":1119,"sourceCode":"      this.lineJoin = tmp.lineJoin;\n    }\n  };\n\n  /**\n   *     Draws a rectangle (no fill)\n   *\n   * @name strokeRect\n   * @function\n   * @param x {Number} The x-coordinate of the upper-left corner of the rectangle\n   * @param y {Number} The y-coordinate of the upper-left corner of the rectangle\n   * @param w {Number} The width of the rectangle, in pixels\n   * @param h {Number} The height of the rectangle, in pixels\n   * @description The strokeRect() method draws a rectangle (no fill). The default color of the stroke is black.\n   */\n  Context2D.prototype.strokeRect = function strokeRect(x, y, w, h) {\n    if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {\n      console.error(\"jsPDF.context2d.strokeRect: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.strokeRect\");\n    }\n    if (isStrokeTransparent.call(this)) {\n      return;\n    }\n    this.beginPath();\n    this.rect(x, y, w, h);\n    this.stroke();\n  };\n\n  /**\n   * Clears the specified pixels within a given rectangle\n   *\n   * @name clearRect\n   * @function\n   * @param x {Number} The x-coordinate of the upper-left corner of the rectangle\n   * @param y {Number} The y-coordinate of the upper-left corner of the rectangle\n   * @param w {Number} The width of the rectangle to clear, in pixels\n   * @param h {Number} The height of the rectangle to clear, in pixels","sourceCodeStart":1101,"sourceCodeEnd":1137,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L1101-L1137","documentation":"strokeRect validates x/y/w/h with isNaN up front and throws before the transparency check or beginPath/rect/stroke pipeline. Even when the stroke style is fully transparent, the guard still fires because validation precedes isStrokeTransparent.","triggerScenarios":"ctx.strokeRect(x, y, w, h) where any argument is undefined, omitted, or non-numeric; computing dimensions from null/undefined layout data.","commonSituations":"Drawing borders around optional panels where dimensions may be absent; passing template-literal numbers; measurement racing producing NaN mid-layout.","solutions":["Coerce to finite numbers and guard with Number.isFinite before calling.","Provide explicit numeric defaults (e.g. 0) for missing dimensions.","Move dimension validation into a shared helper used by rect/fillRect/strokeRect/clearRect."],"exampleFix":"// before\nctx.strokeRect(o.x, o.y, o.w, o.h);\n\n// after\nfunction safeStrokeRect(ctx, x, y, w, h) {\n  if (![x, y, w, h].every(v => Number.isFinite(+v))) return;\n  ctx.strokeRect(+x, +y, +w, +h);\n}","handlingStrategy":"validation","validationCode":"function safeStrokeRect(ctx, x, y, w, h) {\n  const a = [x, y, w, h].map(Number);\n  if (!a.every(Number.isFinite)) return;\n  ctx.strokeRect(...a);\n}","typeGuard":"const isFinite4 = (x,y,w,h) => [x,y,w,h].every(v => typeof v === 'number' && Number.isFinite(v));","tryCatchPattern":"try { ctx.strokeRect(x, y, w, h); }\ncatch (e) { if (!/context2d.strokeRect/.test(e.message)) throw e; }","preventionTips":["Treat border-drawing as optional and skip when dimensions are absent.","Centralize dimension validation so all four rect-family calls share it."],"tags":["canvas2d","context2d","validation","nan","pdf"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}