{"record":{"id":"818b7b1318cefad1","repo":"YMFE/yapi","slug":"hookname","errorCode":null,"errorMessage":"缺少hookname","messagePattern":"缺少hookname","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/plugin.js","lineNumber":195,"sourceCode":"    listener: []\n  },\n\n  /**\n   * addNoticePlugin(config)\n   * \n   * config.weixin = {\n   *    title: 'wechat',\n   *    hander: (emails, title, content)=> {...}\n   * }\n   */\n  addNotice:{\n    type: 'multi',\n    listener: []\n  }\n};\n\nfunction bindHook(name, listener) {\n  if (!name) throw new Error('缺少hookname');\n  if (name in hooks === false) {\n    throw new Error('不存在的hookname');\n  }\n  if (hooks[name].type === 'multi') {\n    hooks[name].listener.push(listener);\n  } else {\n    if (typeof hooks[name].listener === 'function') {\n      throw new Error('重复绑定singleHook(' + name + '), 请检查');\n    }\n    hooks[name].listener = listener;\n  }\n}\n\n/**\n *\n * @param {*} hookname\n * @return promise\n */","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/YMFE/yapi/blob/59bade3a8a43e7db077d38a4b0c7c584f30ddf8c/server/plugin.js#L177-L213","documentation":"bindHook(name, listener) requires a hook name; calling it with an empty/undefined/null name throws this immediately. The hooks registry keys every valid hookname, so a falsy name can never match.","triggerScenarios":"Calling yapi.bindHook('', undefined, null) in a plugin's server.js — typically a typo like bindHook(variable) where the variable is unset, or a mistakenly empty string literal.","commonSituations":"Plugin authors hardcoding wrong constant, config-driven hook names that resolve to empty, copy-paste of bindHook call without filling the first argument.","solutions":["Pass a valid hook name string as first argument (must be one of the hooks registered in server/plugin.js).","Check the variable supplying the name is populated (log it before binding).","Refer to documented hook names e.g. 'add_router', 'interface_save'."],"exampleFix":"// before\nyapi.bindHook(config.hookName, handler); // config.hookName undefined\n// after\nif (config.hookName) yapi.bindHook(config.hookName, handler);","handlingStrategy":"validation","validationCode":"function canBindHook(name) {\n  return typeof name === 'string' && name.length > 0;\n}\nif (canBindHook(hookName)) yapi.bindHook(hookName, handler);","typeGuard":"function isHookName(name) {\n  return typeof name === 'string' && name.trim() !== '';\n}","tryCatchPattern":"try {\n  yapi.bindHook(name, listener);\n} catch (e) {\n  if (e.message === '缺少hookname') console.error('bindHook called without a name');\n  else if (e.message === '不存在的hookname') console.error(`Unknown hook: ${name}`);\n  else throw e;\n}","preventionTips":["Always pass string literals for hook names.","Check the variable supplying the name before binding.","Keep hook name constants in one shared module."],"tags":["plugin","hooks","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"59bade3a8a43e7db077d38a4b0c7c584f30ddf8c","analyzedAt":"2026-08-29T08:45:22.203Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}