{"record":{"id":"2fbab3f98fe57142","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-pubsub-subscribe-jspd","errorCode":null,"errorMessage":"Invalid arguments passed to PubSub.subscribe (jsPDF-module)","messagePattern":"Invalid arguments passed to PubSub\\.subscribe \\(jsPDF-module\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":36,"sourceCode":" * @name PubSub\n * @ignore\n */\nfunction PubSub(context) {\n  if (typeof context !== \"object\") {\n    throw new Error(\n      \"Invalid Context passed to initialize PubSub (jsPDF-module)\"\n    );\n  }\n  var topics = {};\n\n  this.subscribe = function(topic, callback, once) {\n    once = once || false;\n    if (\n      typeof topic !== \"string\" ||\n      typeof callback !== \"function\" ||\n      typeof once !== \"boolean\"\n    ) {\n      throw new Error(\n        \"Invalid arguments passed to PubSub.subscribe (jsPDF-module)\"\n      );\n    }\n\n    if (!topics.hasOwnProperty(topic)) {\n      topics[topic] = {};\n    }\n\n    var token = Math.random().toString(35);\n    topics[topic][token] = [callback, !!once];\n\n    return token;\n  };\n\n  this.unsubscribe = function(token) {\n    for (var topic in topics) {\n      if (topics[topic][token]) {\n        delete topics[topic][token];","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L18-L54","documentation":"PubSub's `subscribe` (src/jspdf.js:29) validates its three parameters: `topic` must be a string, `callback` must be a function, and `once` must be a boolean. The check at src/jspdf.js:31-35 fires before any registration happens, so a bad subscription is rejected cleanly rather than producing a silent no-op token. End users normally subscribe through higher-level event helpers; the error typically comes from a plugin or integration code that hands a non-function callback, a numeric topic, or a truthy non-boolean `once`.","triggerScenarios":"Calling `events.subscribe(123, fn)` (non-string topic); `events.subscribe('addPage', null)` or passing the result of an expression that is undefined as callback; `events.subscribe('addPage', fn, 'true')` where once is a string rather than boolean; a plugin forwarding user-supplied values into subscribe without coercion.","commonSituations":"Plugin authors registering listeners with a topic pulled from dynamic config that is sometimes undefined; passing a method reference that lost its `this` binding and resolved to undefined; deserialized JSON config where a callback key is missing; version mismatches where a plugin expects an extra parameter and passes garbage as `once`.","solutions":["Verify the topic is a non-empty string before subscribing (`typeof topic === 'string'`).","Ensure the callback is a real function reference (`typeof callback === 'function'`) — not `callback()` (which calls it) or an undefined property access.","Omit `once` or pass an explicit boolean; rely on the `once = once || false` default rather than passing a string/number.","If subscribing from user-supplied config, coerce and validate all three fields before the call."],"exampleFix":"// before\ninternal.events.subscribe(topic, handler, handler.once || 'yes');\n\n// after\ninternal.events.subscribe(String(topic), handler, Boolean(handler.once));","handlingStrategy":"validation","validationCode":"function safeSubscribe(events, topic, cb, once) {\n  if (typeof topic !== 'string') throw new TypeError('topic must be a string');\n  if (typeof cb !== 'function') throw new TypeError('callback must be a function');\n  return events.subscribe(topic, cb, once === true);\n}","typeGuard":"function isValidSubscription(topic, cb, once) {\n  return typeof topic === 'string' &&\n         typeof cb === 'function' &&\n         typeof once === 'boolean';\n}","tryCatchPattern":"try {\n  token = events.subscribe(topic, handler, Boolean(once));\n} catch (e) {\n  if (/Invalid arguments passed to PubSub.subscribe/.test(e.message)) {\n    console.warn('Skipping invalid subscription for topic', topic);\n  } else throw e;\n}","preventionTips":["Always pass a real function reference as the callback — never the result of calling it.","Coerce `once` to a boolean explicitly.","Validate topic and callback types before subscribing when values come from dynamic config."],"tags":["pubsub","events","subscribe","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}