{"record":{"id":"f967cc627833a781","repo":"parallax/jsPDF","slug":"error-in-function-function-name-message","errorCode":null,"errorMessage":"Error in function {function_name}: {message}","messagePattern":"Error in function (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":2505,"sourceCode":"  };\n\n  var SAFE = function __safeCall(fn) {\n    fn.foo = function __safeCallWrapper() {\n      try {\n        return fn.apply(this, arguments);\n      } catch (e) {\n        var stack = e.stack || \"\";\n        if (~stack.indexOf(\" at \")) stack = stack.split(\" at \")[1];\n        var m =\n          \"Error in function \" +\n          stack.split(\"\\n\")[0].split(\"<\")[0] +\n          \": \" +\n          e.message;\n        if (globalObject.console) {\n          globalObject.console.error(m, e);\n          if (globalObject.alert) alert(m);\n        } else {\n          throw new Error(m);\n        }\n      }\n    };\n    fn.foo.bar = fn;\n    return fn.foo;\n  };\n\n  var to8bitStream = function(text, flags) {\n    /**\n     * PDF 1.3 spec:\n     * \"For text strings encoded in Unicode, the first two bytes must be 254 followed by\n     * 255, representing the Unicode byte order marker, U+FEFF. (This sequence conflicts\n     * with the PDFDocEncoding character sequence thorn ydieresis, which is unlikely\n     * to be a meaningful beginning of a word or phrase.) The remainder of the\n     * string consists of Unicode character codes, according to the UTF-16 encoding\n     * specified in the Unicode standard, version 2.0. Commonly used Unicode values\n     * are represented as 2 bytes per character, with the high-order byte appearing first\n     * in the string.\"","sourceCodeStart":2487,"sourceCodeEnd":2523,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L2487-L2523","documentation":"This is not a distinct failure mode but the SAFE wrapper (`__safeCall`, src/jspdf.js:2489) that wraps `output()` (src/jspdf.js:3075). When any exception occurs during PDF generation, it builds a message `'Error in function <name>: <original message>'` from the stack. In a browser (`globalObject.console` present) it logs via console.error and alerts the user; it only re-throws (src/jspdf.js:2505) when there is no console — e.g. in stripped-down/embedded runtimes. So the real cause is whatever exception `e` was; this wrapper is the reporting layer.","triggerScenarios":"Any error thrown synchronously inside `doc.output(...)` (e.g. a font-not-found, a NaN in geometry, a plugin error) surfaces wrapped as `Error in function output: <msg>`; in a console-less runtime the wrapped error is re-thrown and propagates to the caller.","commonSituations":"Node/embedded environments where `globalObject.console` is undefined, so the error is thrown instead of logged; calling output() before any required setup (fonts, pages); a plugin throwing during the output render pass.","solutions":["Read the inner `<original message>` after the colon — it names the real failure (font, NaN, encryption, etc.); fix that root cause.","Wrap `doc.output(...)` in try/catch so generation failures don't crash the host app.","Ensure a console/global is available if you rely on the log-and-alert path.","Validate fonts/pages/options before calling output() to avoid known failure modes."],"exampleFix":"// before\n var data = doc.output('arraybuffer');  // unhandled wrapped error\n\n// after\n try {\n   var data = doc.output('arraybuffer');\n } catch (e) {\n   console.error('PDF generation failed:', e.message);\n   // handle gracefully\n }","handlingStrategy":"try-catch","validationCode":"// No pre-API check possible (the wrapper reports arbitrary inner errors).\n// Best prevention: validate inputs before output() — fonts registered, at least one page, finite coordinates.\nfunction preflight(doc) {\n  if (!doc.getNumberOfPages || doc.getNumberOfPages() < 1) throw new Error('No pages');\n  return true;\n}\npreflight(doc);\nvar data = doc.output('arraybuffer');","typeGuard":"function isOutputReady(doc) {\n  return typeof doc.output === 'function' && doc.getNumberOfPages() > 0;\n}","tryCatchPattern":"try {\n  var data = doc.output('arraybuffer');\n} catch (e) {\n  // e.message is 'Error in function output: <inner message>'\n  var inner = e.message.replace(/^Error in function [^:]+: /, '');\n  console.error('PDF output failed:', inner);\n  // recover or report inner cause to the user\n}","preventionTips":["Always wrap doc.output() in try/catch so generation errors don't crash the app.","Read the substring after the colon for the real root cause and fix that.","Ensure a console/global is available if you rely on the log-and-alert path.","Register fonts and add at least one page before calling output."],"tags":["output","wrapper","error-handling","reporting"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}