angular/angular.js · error · Error

Unknown mode '{}', only 'log'/'rethrow' modes are allowed!

Error message

Unknown mode '{}', only 'log'/'rethrow' modes are allowed!

What it means

ngMock replaces the $log provider with one whose mode(mode) setter accepts exactly two strategies: 'log' (collect every message into per-level arrays) and 'rethrow' (collect and also rethrow errors). Any other truthy value falls through the switch to the default branch and throws, so misconfiguration fails fast at setup time instead of silently doing nothing.

Source

Thrown at src/ngMock/angular-mocks.js:403

    switch (mode) {
      case 'log':
      case 'rethrow':
        var errors = [];
        handler = function(e) {
          if (arguments.length === 1) {
            errors.push(e);
          } else {
            errors.push([].slice.call(arguments, 0));
          }
          if (mode === 'rethrow') {
            throw e;
          }
        };
        handler.errors = errors;
        break;
      default:
        throw new Error('Unknown mode \'' + mode + '\', only \'log\'/\'rethrow\' modes are allowed!');
    }
  };

  this.$get = function() {
    return handler;
  };

  this.mode('rethrow');
};


/**
 * @ngdoc service
 * @name $log
 *
 * @description
 * Mock implementation of {@link ng.$log} that gathers all logged messages in arrays
 * (one array per logging level). These arrays are exposed as `logs` property of each of the

View on GitHub (pinned to d8f77817eb)

Solutions

  1. Use 'log' to collect messages for later assertions, or 'rethrow' to also make logged errors fail the test.
  2. To suppress console noise in tests, do not invent a mode — use $log.reset() after asserting, or $logProvider.debugEnabled(false) for debug-level output.
  3. Check for typos/casing in the mode string ('Log', 'RETHROW' will throw — the comparison is exact).

Example fix

// before
beforeEach(module(function($logProvider) {
  $logProvider.mode('silent'); // throws: unknown mode
}));

// after
beforeEach(module(function($logProvider) {
  $logProvider.mode('log'); // collect into $log.error.logs etc.
}));
Defensive patterns

Strategy: type-guard

Validate before calling

var VALID_LOG_MODES = ['log', 'rethrow'];
if (VALID_LOG_MODES.indexOf(mode) === -1) {
  throw new Error('mode must be one of ' + VALID_LOG_MODES.join('/') + ', got: ' + mode);
}
$logProvider.mode(mode);

Type guard

function isValidLogMode(mode) {
  return mode === 'log' || mode === 'rethrow';
}

Prevention

When it happens

Trigger: Calling $logProvider.mode() (or the mock's provider) with a typo or invented name such as 'throw', 'silent', 'none', 'warn', 'strict'; passing a variable whose value is an unexpected string.

Common situations: Trying to silence noisy test output with an imagined mode like 'silent' or 'off'; porting test setup from another logging library whose mode names differ; passing undefined is tolerated (the setter only switches when mode is truthy), so only concrete wrong strings trigger it.

Related errors


AI-assisted analysis of angular/angular.js@d8f77817eb (2026-08-21). Data as JSON: /api/errors/a184cd73811246df. Report an issue: GitHub.