pear-devs/pear-desktop · error · TypeError

Expected 'linear', 'logarithmic' or a positive number as fad

Error message

Expected 'linear', 'logarithmic' or a positive number as fade scaling preference!

What it means

VolumeFader's options.fadeScaling accepts only the string 'linear', the string 'logarithmic' (or undefined, both meaning 60 dB default), or a positive finite number interpreted as a custom dynamic range in amplitude-dB. Any other value — strings like 'log', 0, negative numbers, NaN, null — throws this TypeError from the constructor.

Source

Thrown at src/plugins/crossfade/fader.ts:141

        options.fadeScaling === undefined ||
        options.fadeScaling === 'logarithmic'
      ) {
        // Set default of 60 dB
        dynamicRange = 3;
      }
      // Custom dynamic range?
      else if (
        typeof options.fadeScaling === 'number' &&
        !Number.isNaN(options.fadeScaling) &&
        options.fadeScaling > 0
      ) {
        // Turn amplitude dB into a multiple of 10 power dB
        dynamicRange = options.fadeScaling / 2 / 10;
      }
      // Unsupported value
      else {
        // Abort and throw exception
        throw new TypeError(
          "Expected 'linear', 'logarithmic' or a positive number as fade scaling preference!",
        );
      }

      // Use exponential/logarithmic scaler for expansion/compression
      this.scale = {
        internalToVolume: (level: number) =>
          this.exponentialScaler(level, dynamicRange),
        volumeToInternal: (level: number) =>
          this.logarithmicScaler(level, dynamicRange),
      };

      // Log setting if not default
      if (options.fadeScaling)
        this.logger?.(
          'Using logarithmic fading with ' +
            String(10 * dynamicRange) +
            ' dB dynamic range.',

View on GitHub (pinned to 1e2aac5706)

Solutions

  1. Use exactly 'linear', 'logarithmic', or a positive number (e.g. 60 for 60 dB range)
  2. Sanitize user config before constructing: trim/lowercase strings and reject or default unknown values
  3. Validate with a helper (see type guard) so bad settings fall back to the logarithmic default instead of crashing the player

Example fix

// before
new VolumeFader(media, { fadeScaling: 'log' }); // throws

// after
const scaling = ['linear','logarithmic'].includes(cfg.fadeScaling) || (typeof cfg.fadeScaling === 'number' && cfg.fadeScaling > 0)
  ? cfg.fadeScaling
  : 'logarithmic';
new VolumeFader(media, { fadeScaling: scaling });
Defensive patterns

Strategy: validation

Validate before calling

const FADE_SCALINGS = ['linear', 'logarithmic'] as const;
const parseScaling = (v: unknown) =>
  v === 'linear' || v === 'logarithmic' ? v
  : typeof v === 'number' && Number.isFinite(v) && v > 0 ? v
  : 'logarithmic'; // safe default

Type guard

const isFadeScaling = (v: unknown): v is 'linear' | 'logarithmic' | number =>
  v === 'linear' || v === 'logarithmic' ||
  (typeof v === 'number' && Number.isFinite(v) && v > 0);

Try / catch

try { new VolumeFader(media, { fadeScaling: cfg.fadeScaling }); } catch (e) { if (e instanceof TypeError && /fade scaling/.test(e.message)) new VolumeFader(media); /* defaults */ else throw e; }

Prevention

When it happens

Trigger: Passing {fadeScaling: 'log'} or {fadeScaling: 'exp'}; passing 0 or a negative dB value; passing NaN or null because of an unguarded config field; typos in config keys from user settings files.

Common situations: User-editable plugin settings where a free-text scaling preference is typed in ('LOG', 'logarithmic ' with whitespace); migrating config between versions where the value became null; copy-pasting example config that uses an unsupported shorthand.

Related errors


AI-assisted analysis of pear-devs/pear-desktop@1e2aac5706 (2026-08-27). Data as JSON: /api/errors/741d00ab60ee05eb. Report an issue: GitHub.