dianping/cat · error · Error

alert requires callback property to be a function when provi

Error message

alert requires callback property to be a function when provided

What it means

exports.alert validates that, if a callback is supplied, it is a function. alert is fire-and-forget — the callback (if any) is invoked when the dialog closes — so anything non-callable (string, number, object) is rejected rather than silently ignored.

Source

Thrown at cat-home/src/main/webapp/assets/js/uncompressed/bootbox.js:307

      allowedButtons[value] = true;
    });

    each(options.buttons, function(key) {
      if (allowedButtons[key] === undefined) {
        throw new Error("button key " + key + " is not allowed (options are " + buttons.join("\n") + ")");
      }
    });

    return options;
  }

  exports.alert = function() {
    var options;

    options = mergeDialogOptions("alert", ["ok"], ["message", "callback"], arguments);

    if (options.callback && !$.isFunction(options.callback)) {
      throw new Error("alert requires callback property to be a function when provided");
    }

    /**
     * overrides
     */
    options.buttons.ok.callback = options.onEscape = function() {
      if ($.isFunction(options.callback)) {
        return options.callback();
      }
      return true;
    };

    return exports.dialog(options);
  };

  exports.confirm = function() {
    var options;

View on GitHub (pinned to e815e74d4c)

Solutions

  1. Pass a function or omit the second argument entirely: bootbox.alert('Done'); bootbox.alert('Done', function(){ ... });
  2. If you need to pass data, close over it: bootbox.alert('Done ' + name, function(){ refresh(name); }).
  3. If wrapping alert dynamically, guard: typeof cb === 'function' ? bootbox.alert(m, cb) : bootbox.alert(m).

Example fix

// before
bootbox.alert('Saved', { onClose: reloadList });

// after
bootbox.alert('Saved', reloadList);
Defensive patterns

Strategy: type-guard

Validate before calling

bootbox.alert('Saved', typeof cb === 'function' ? cb : undefined);
// undefined second arg is safely omitted by the wrapper

Type guard

function isOptionalCallback(cb) {
  return cb === undefined || typeof cb === 'function';
}

Prevention

When it happens

Trigger: bootbox.alert('Done', 'refresh'); bootbox.alert('Done', {onClose: fn}); passing a variable that is undefined-checked but assigned a non-function; passing a method reference that failed to bind and came back as a string.

Common situations: Refactoring from a config-object style where callback lived inside an object; passing extra data as a second argument forgetting the second arg is the callback; minification/renaming that left the callback undefined-turned-string.

Related errors


AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14). Data as JSON: /api/errors/7813b831bdc50162. Report an issue: GitHub.