badges/shields · error · InvalidResponse

"${preferredThreshold}" threshold missing

Error message

"${preferredThreshold}" threshold missing

What it means

When a valid preferredThreshold is given, the nycrc service looks up that key (branches/lines/functions) in the parsed nyc/c8 config and throws InvalidResponse if the key is absent or falsy. This means the user's package.json or .nycrc does not define the threshold they explicitly asked the badge to display.

Source

Thrown at services/nycrc/nycrc.service.js:95

  static render({ coverage }) {
    return {
      message: `${coverage.toFixed(0)}%`,
      color: coveragePercentage(coverage),
    }
  }

  extractThreshold(config, preferredThreshold) {
    const { branches, lines } = config
    if (preferredThreshold) {
      if (!validThresholds.includes(preferredThreshold)) {
        throw new InvalidParameter({
          prettyMessage:
            'threshold must be "branches", "lines", or "functions"',
        })
      }
      if (!config[preferredThreshold]) {
        throw new InvalidResponse({
          prettyMessage: `"${preferredThreshold}" threshold missing`,
        })
      }
      return config[preferredThreshold]
    } else if (branches || lines) {
      // We favor branches over lines for the coverage badge, if both
      // thresholds are provided (as branches is the stricter requirement):
      return branches || lines
    } else {
      throw new InvalidResponse({
        prettyMessage: '"branches" or "lines" threshold missing',
      })
    }
  }

  async handle({ user, repo }, queryParams) {
    const { config, preferredThreshold } = queryParams
    let coverage

View on GitHub (pinned to 766fd8bc89)

Solutions

  1. Add the requested threshold key (e.g. "functions": 80) to the nyc/c8 config in package.json or .nycrc.
  2. Or change the badge's preferredThreshold parameter to a threshold the config actually defines.
  3. Ensure the threshold value is non-zero, since a falsy value also triggers this error.

Example fix

// before (package.json)
"nyc": { "lines": 90 }
// after
"nyc": { "lines": 90, "functions": 85 }
Defensive patterns

Strategy: validation

Validate before calling

const cfg = require('./package.json').nyc || require('./package.json').c8
if (!cfg || !cfg[preferredThreshold]) {
  throw new Error(`Config does not define a ${preferredThreshold} threshold`)
}

Type guard

function hasThreshold(cfg, key) {
  return cfg != null && typeof cfg === 'object' &&
    typeof cfg[key] === 'number' && cfg[key] > 0
}

Prevention

When it happens

Trigger: Badge request specifies e.g. preferred_threshold=functions, but the project's nyc/c8 stanza has no `functions` threshold configured (`!config[preferredThreshold]` is true).

Common situations: Configs that only set `lines`/`branches` thresholds while the badge asks for `functions`; thresholds set to 0 or omitted in package.json `nyc` block; switching badge param without updating .nycrc.

Related errors


AI-assisted analysis of badges/shields@766fd8bc89 (2026-08-30). Data as JSON: /api/errors/8f6eb2734a2dedb9. Report an issue: GitHub.