usebruno/bruno · warning · Error

Failed to minify

Error message

Failed to minify

What it means

`bru.utils.minifyJson` (and identically `minifyXml`) throw the bare `Failed to minify` only when the input is `null` or `undefined`; later branches add `: ${err.message}` for parse failures. It is a null-input guard on the test-script helper.

Source

Thrown at packages/bruno-js/src/bru.js:101

    this._runtimeVarsDirty = false;
    // Holds credential IDs to be reset after script execution
    this.oauth2CredentialsToReset = [];
    this.runner = {
      skipRequest: () => {
        this.skipRequest = true;
      },
      stopExecution: () => {
        this.stopExecution = true;
      },
      setNextRequest: (nextRequest) => {
        this.nextRequest = nextRequest;
      }
    };

    this.utils = {
      minifyJson: (json) => {
        if (json === null || json === undefined) {
          throw new Error('Failed to minify');
        }

        if (typeof json === 'object') {
          try {
            return JSON.stringify(json);
          } catch (err) {
            throw new Error(`Failed to minify: ${err?.message || err}`);
          }
        }

        if (typeof json === 'string') {
          const trimmed = json.trim();
          if (trimmed === '') return trimmed;
          try {
            return JSON.stringify(JSON.parse(trimmed));
          } catch (err) {
            throw new Error(`Failed to minify: ${err?.message || err}`);
          }

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. Guard the call: `if (x != null) bru.utils.minifyJson(x)`.
  2. Default to a safe value: `bru.utils.minifyJson(x ?? {})` for JSON or `x ?? ''` for XML.
  3. Log the variable before calling to find which upstream field is null.
  4. Use `typeof x !== 'undefined'` checks for variables that may be undeclared.

Example fix

// before
const min = bru.utils.minifyJson(req.body);   // req.body is null

// after
const min = req.body == null ? '' : bru.utils.minifyJson(req.body);
Defensive patterns

Strategy: validation

Validate before calling

function safeMinifyJson(x) {
  if (x === null || x === undefined) return '';
  return bru.utils.minifyJson(x);
}
const min = safeMinifyJson(maybeNullBody);

Type guard

function isMinifiable(v: unknown): v is string | object {
  return v !== null && v !== undefined && (typeof v === 'string' || typeof v === 'object');
}

Try / catch

try { bru.utils.minifyJson(x); }
catch (e) { if (/^Failed to minify$/.test(e.message)) { /* input was null/undefined; skip */ } else throw e; }

Prevention

When it happens

Trigger: Calling `bru.utils.minifyJson(null)` or `bru.utils.minifyJson(undefined)` (or `minifyXml(null/undefined)`) inside a Bruno test/pre-request script.

Common situations: Script reads `req.body` or a parsed JSON var that is null (no body set, JSON.parse returned null, or a missing response field) and pipes it straight into `minifyJson`; an unset variable; a conditional response shape.

Related errors


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/a73acf18523acdfc. Report an issue: GitHub.