agalwood/Motrix · error · RangeError
${label} must be finite and non-negative
Error message
${label} must be finite and non-negative What it means
RangeError from normalizeSpeed when value is not finite (NaN or +/-Infinity) or is negative. Speeds must be a finite non-negative number before rounding to a safe integer; otherwise the normalized result would be meaningless.
Source
Thrown at src/core/inspector-activity/validators.ts:79
value: number,
label: string
): number {
if (!Number.isSafeInteger(value) || value < 0) {
throw new RangeError(`${label} must be a non-negative safe integer`)
}
return value
}
export function assertNonNegativeBigInt(value: bigint, label: string): bigint {
if (typeof value !== 'bigint' || value < 0n) {
throw new RangeError(`${label} must be a non-negative bigint`)
}
return value
}
export function normalizeSpeed(value: number, label: string): number {
if (!Number.isFinite(value) || value < 0) {
throw new RangeError(`${label} must be finite and non-negative`)
}
const normalized = Math.round(value)
if (!Number.isSafeInteger(normalized)) {
throw new RangeError(`${label} exceeds the JavaScript safe integer range`)
}
return normalized
}
export function saturatingAddSignedInt64(
current: bigint,
delta: bigint
): { value: bigint; saturated: boolean } {
assertNonNegativeBigInt(current, 'current')
assertNonNegativeBigInt(delta, 'delta')
if (current > MAX_SIGNED_SQLITE_INTEGER) {
throw new RangeError('current exceeds the signed int64 range')
}
if (delta > MAX_SIGNED_SQLITE_INTEGER - current) {View on GitHub (pinned to 1a708ee577)
Solutions
- Guard divisions: compute speed only when elapsed > 0.
- Coerce non-finite/negative to 0 before normalizing.
- Treat Infinity/NaN upstream as 'no data' and skip the call.
Example fix
// before const speed = bytes / elapsed // elapsed may be 0 -> Infinity return normalizeSpeed(speed, 'downloadSpeed') // after const speed = elapsed > 0 ? bytes / elapsed : 0 return normalizeSpeed(speed < 0 || !Number.isFinite(speed) ? 0 : speed, 'downloadSpeed')
Defensive patterns
Strategy: validation
Validate before calling
// Eliminate NaN/Infinity/negative before normalizing a speed.
function safeSpeed(bytes: number, elapsedMs: number): number {
if (elapsedMs <= 0 || !Number.isFinite(bytes)) return 0
const s = bytes / (elapsedMs / 1000)
return Number.isFinite(s) && s >= 0 ? s : 0
}
return normalizeSpeed(safeSpeed(bytes, elapsed), 'downloadSpeed') Type guard
function isFiniteNonNegative(v: unknown): v is number {
return typeof v === 'number' && Number.isFinite(v) && v >= 0
} Try / catch
try {
return normalizeSpeed(value, 'downloadSpeed')
} catch (err) {
if (err instanceof RangeError && /finite and non-negative/.test(err.message)) {
return 0 // unknown speed is reported as 0
}
throw err
} Prevention
- Guard all speed divisions with elapsed > 0.
- Treat NaN/Infinity upstream as 'no data' -> 0.
- Clamp negatives to 0 at the producer.
- Validate before passing computed rates to normalizeSpeed.
When it happens
Trigger: normalizeSpeed(value, label) with !Number.isFinite(value) || value < 0. E.g. NaN (from 0/0 or failed parse), Infinity, -1, or a negative float.
Common situations: A speed computed from bytes/time where time was 0 (division -> Infinity/NaN); a parsing failure yielding NaN; a sign error producing a negative rate; an upstream unit returning Infinity for 'unknown'.
Related errors
- ${label} must be a positive safe integer
- ${label} must be a non-negative safe integer
- taskId must be a string
- taskId must contain between 1 and ${MAX_TASK_ID_LENGTH} char
- ${label} must be a non-negative bigint
AI-assisted analysis of agalwood/Motrix@1a708ee577 (2026-08-12).
Data as JSON: /api/errors/5581bbf479d578b3.
Report an issue: GitHub.