{"record":{"id":"3fe4cd20b85806fb","repo":"nwjs/nw.js","slug":"failed-must-be-a-valid-function","errorCode":null,"errorMessage":"'failed' must be a valid function.","messagePattern":"'failed' must be a valid function\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/api/shortcut/shorcut.js","lineNumber":42,"sourceCode":"  if (typeof option != 'object')\n    throw new TypeError('Invalid option.');\n\n  if (!option.hasOwnProperty('key'))\n    throw new TypeError(\"Shortcut requires 'key' to specify key combinations.\");\n\n  option.key = String(option.key);\n  this.key = option.key;\n\n  if (option.hasOwnProperty('active')) {\n    if (typeof option.active != 'function')\n      throw new TypeError(\"'active' must be a valid function.\");\n    else\n      this.active = option.active;\n  }\n\n  if (option.hasOwnProperty('failed')) {\n    if (typeof option.failed != 'function')\n      throw new TypeError(\"'failed' must be a valid function.\");\n    else\n      this.failed = option.failed;\n  }\n\n  v8_util.setHiddenValue(this, 'option', option);\n  nw.allocateObject(this, option);\n}\n\nrequire('util').inherits(Shortcut, exports.Base);\n\nShortcut.prototype.handleEvent = function(ev) {\n  if (ev == 'active') {\n    if (typeof this.active == 'function')\n      this.active();\n  } else if (ev == 'failed') {\n    if (typeof this.failed == 'function')\n      this.failed(arguments[1]);\n  }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/nwjs/nw.js/blob/e15da848e9e08e6e467532dae78995c6ad2f55ee/src/api/shortcut/shorcut.js#L24-L60","documentation":"Thrown by the nw.Shortcut constructor when the 'failed' option is present but is not a function. 'failed' is the callback invoked when the accelerator cannot be registered (e.g. the key combo is taken by the OS), so it must be callable.","triggerScenarios":"Calling `new nw.Shortcut({ key: 'Ctrl+A', failed: null })`, `failed: false`, or `failed: someObj.method` where `this` binding produced undefined.","commonSituations":"Copy-paste from a config that set failed to a boolean flag; passing a value from an optional field that defaulted to a non-function.","solutions":["Provide a real function for 'failed' or omit it entirely.","Guard with typeof before passing: `if (typeof cb === 'function') opt.failed = cb;`."],"exampleFix":"// before\nnew nw.Shortcut({ key: 'Ctrl+A', failed: false });\n// after\nnew nw.Shortcut({ key: 'Ctrl+A', failed: (err) => console.error(err) });","handlingStrategy":"type-guard","validationCode":"if (option && option.hasOwnProperty('failed') && typeof option.failed !== 'function') {\n  throw new Error('option.failed must be a function');\n}","typeGuard":"function isCallable(v) { return typeof v === 'function'; }","tryCatchPattern":"try { new nw.Shortcut(opt); } catch (e) { if (/failed/.test(e.message)) logger.warn(e.message); else throw e; }","preventionTips":["Omit 'failed' when you do not need error handling rather than passing a placeholder.","Resolve handler references before building the option object."],"tags":["shortcut","constructor","type-check","nwjs"],"backgroundTag":null,"analyzedSha":"e15da848e9e08e6e467532dae78995c6ad2f55ee","analyzedAt":"2026-08-13T04:15:35.452Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}