{"record":{"id":"f3d54fb5452e54d1","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-rect","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.rect","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.rect","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":1053,"sourceCode":"  Context2D.prototype.arcTo = function(x1, y1, x2, y2, radius) {\n    throw new Error(\"arcTo not implemented.\");\n  };\n\n  /**\n   * Creates a rectangle\n   *\n   * @name rect\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 rect() method creates a rectangle.\n   */\n  Context2D.prototype.rect = function(x, y, w, h) {\n    if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {\n      console.error(\"jsPDF.context2d.rect: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.rect\");\n    }\n    this.moveTo(x, y);\n    this.lineTo(x + w, y);\n    this.lineTo(x + w, y + h);\n    this.lineTo(x, y + h);\n    this.lineTo(x, y);\n    this.lineTo(x + w, y);\n    this.lineTo(x, y);\n  };\n\n  /**\n   * Draws a \"filled\" rectangle\n   *\n   * @name fillRect\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","sourceCodeStart":1035,"sourceCodeEnd":1071,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L1035-L1071","documentation":"jsPDF.context2d.rect validates its four numeric arguments with isNaN() before building the rectangle path. Because isNaN(undefined) is true, omitting any argument (or passing a non-numeric value) trips the guard. The console.error logs the raw arguments object, then a descriptive Error is thrown, halting path construction.","triggerScenarios":"Calling ctx.rect() with fewer than four arguments; passing a string, null-undefined variable, or NaN for x/y/w/h; reading dimensions from a malformed config object whose fields are undefined.","commonSituations":"Dynamic layouts where a layout field is missing and defaults are not applied; passing CSS-pixel strings ('10px') instead of numbers; math that divides by zero producing NaN dimensions.","solutions":["Pass four finite numbers — coerce with Number() and apply defaults: ctx.rect(x|0, y|0, w||0, h||0).","Validate inputs upstream and skip the rect call when any dimension is missing or non-numeric.","Strip units/strings before calling (parseFloat('100px') => 100)."],"exampleFix":"// before\nctx.rect(box.x, box.y, box.w, box.h); // throws if any field is undefined\n\n// after\nconst r = ['x','y','w','h'].map(k => Number(box[k]));\nif (r.every(v => Number.isFinite(v))) ctx.rect(...r);","handlingStrategy":"validation","validationCode":"function safeRect(ctx, x, y, w, h) {\n  const [nx, ny, nw, nh] = [x, y, w, h].map(Number);\n  if (![nx, ny, nw, nh].every(Number.isFinite)) {\n    console.warn('rect skipped: non-finite args', { x, y, w, h });\n    return;\n  }\n  ctx.rect(nx, ny, nw, nh);\n}","typeGuard":"function isRectArgs(x, y, w, h) {\n  return [x, y, w, h].every(v => typeof v === 'number' && Number.isFinite(v));\n}","tryCatchPattern":"try { ctx.rect(x, y, w, h); }\ncatch (e) {\n  if (/Invalid arguments passed to jsPDF.context2d.rect/.test(e.message)) {\n    // coerce/fix and retry with finite numbers, or skip\n  } else throw e;\n}","preventionTips":["Always pass four explicit finite numbers; never rely on undefined defaulting.","Coerce config values with Number() at the boundary where data enters your render code.","Share one validation helper across rect/fillRect/strokeRect/clearRect."],"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"}