YMFE/yapi · error

不存在的hookname

Error message

不存在的hookname

What it means

bindHook validates the name against the known hooks map; if the hookname is not registered it throws '不存在的hookname'. YApi only supports a fixed set of hook points, so plugins must bind to existing ones.

Source

Thrown at server/plugin.js:197

  /**
   * addNoticePlugin(config)
   * 
   * config.weixin = {
   *    title: 'wechat',
   *    hander: (emails, title, content)=> {...}
   * }
   */
  addNotice:{
    type: 'multi',
    listener: []
  }
};

function bindHook(name, listener) {
  if (!name) throw new Error('缺少hookname');
  if (name in hooks === false) {
    throw new Error('不存在的hookname');
  }
  if (hooks[name].type === 'multi') {
    hooks[name].listener.push(listener);
  } else {
    if (typeof hooks[name].listener === 'function') {
      throw new Error('重复绑定singleHook(' + name + '), 请检查');
    }
    hooks[name].listener = listener;
  }
}

/**
 *
 * @param {*} hookname
 * @return promise
 */
function emitHook(name) {
  if (hooks[name] && typeof hooks[name] === 'object') {

View on GitHub (pinned to 59bade3a8a)

Solutions

  1. Use an existing hook name from the hooks registry in server/plugin.js.
  2. Check YApi version compatibility — hook names change across versions.
  3. Grep the codebase for 'bindHook(' usages to see valid names.

Example fix

// before
yapi.bindHook('interface_add', handler); // not a real hook
// after
yapi.bindHook('add_router', handler);
Defensive patterns

Strategy: validation

Validate before calling

const knownHooks = ['add_router', 'interface_save', 'project_add', 'init', ...]; // from server/plugin.js
if (!knownHooks.includes(name)) throw new Error(`Unknown hook: ${name}`);

Type guard

function isRegisteredHook(name, hooks) {
  return typeof name === 'string' && name in hooks;
}

Try / catch

try {
  yapi.bindHook(name, listener);
} catch (e) {
  if (e.message === '不存在的hookname') {
    console.error(`Hook "${name}" not supported in this YApi version`);
  } else { throw e; }
}

Prevention

When it happens

Trigger: yapi.bindHook('some_typo_or_invented_hook', fn) where the name is absent from the hooks object in server/plugin.js.

Common situations: Plugin written for a different YApi version whose hook names differ; misspelled hook name; copying hook names from docs of another project.

Related errors


AI-assisted analysis of YMFE/yapi@59bade3a8a (2026-08-29). Data as JSON: /api/errors/7a733471ba8ee7ea. Report an issue: GitHub.