{"record":{"id":"672f8bbb886737d6","repo":"tursodatabase/turso","slug":"only-finite-numbers-not-infinity-or-nan-can-be-p","errorCode":null,"errorMessage":"Only finite numbers (not Infinity or NaN) can be passed as arguments","messagePattern":"Only finite numbers \\(not Infinity or NaN\\) can be passed as arguments","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"serverless/javascript/src/protocol.ts","lineNumber":119,"sourceCode":"    error?: {\n      message: string;\n      code: string;\n    };\n  }>;\n}\n\nfunction toBase64(uint8: Uint8Array): string {\n  return Buffer.from(uint8.buffer, uint8.byteOffset, uint8.byteLength).toString('base64');\n}\n\nexport function encodeValue(value: any): Value {\n  if (value === null || value === undefined) {\n    return { type: 'null' };\n  }\n  \n  if (typeof value === 'number') {\n    if (!Number.isFinite(value)) {\n      throw new Error(\"Only finite numbers (not Infinity or NaN) can be passed as arguments\");\n    }\n    if (Number.isSafeInteger(value)) {\n      return { type: 'integer', value: value.toString() };\n    }\n    return { type: 'float', value };\n  }\n  \n  if (typeof value === 'bigint') {\n    return { type: 'integer', value: value.toString() };\n  }\n  \n  if (typeof value === 'boolean') {\n    return { type: 'integer', value: value ? '1' : '0' };\n  }\n  \n  if (typeof value === 'string') {\n    return { type: 'text', value };\n  }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/serverless/javascript/src/protocol.ts#L101-L137","documentation":"Thrown by the wire encoder (encodeValue in protocol.ts) when a bound parameter is a number that is not finite — NaN, Infinity, or -Infinity. SQLite has no storage representation for these values, so the driver rejects them client-side before any request is sent; use null, or store the float as text, instead.","triggerScenarios":"Binding a computed value that is NaN (Number(undefined), parseFloat(''), undefined + 1) or Infinity (x / 0, Number.MAX_VALUE overflow); passing Number.POSITIVE_INFINITY / Number.NaN explicitly as an argument to run/get/all/batch/execute.","commonSituations":"Averaging an empty array (sum/count === NaN when count is 0); optional fields whose value is undefined reaching the bind call; parseFloat on user input that is not numeric; ratio metrics dividing by a zero denominator.","solutions":["Fix the upstream computation so it cannot produce NaN/Infinity (guard empty arrays, validate parseFloat results, check divisors)","Coerce unusable numbers to null before binding: db.run(sql, Number.isFinite(v) ? v : null)","Validate parameter arrays with Number.isFinite() before executing the statement"],"exampleFix":"// before\nconst ratio = total / count; // NaN when count === 0\nawait db.run(\"INSERT INTO stats(ratio) VALUES (?)\", ratio);\n\n// after\nconst ratio = count === 0 ? null : total / count;\nawait db.run(\"INSERT INTO stats(ratio) VALUES (?)\", ratio);","handlingStrategy":"validation","validationCode":"const safe = (v: unknown) =>\n  typeof v === \"number\" && !Number.isFinite(v) ? null : v;\nawait db.run(\"INSERT INTO metrics(ratio) VALUES (?)\", safe(total / count));","typeGuard":"const isFiniteNumber = (v: unknown): v is number =>\n  typeof v === \"number\" && Number.isFinite(v);","tryCatchPattern":"try {\n  await stmt.run([value]);\n} catch (e) {\n  if (e instanceof Error && e.message.includes(\"Only finite numbers\")) {\n    await stmt.run([null]); // or fix the upstream computation\n  } else throw e;\n}","preventionTips":["Guard divisions and averages: empty arrays yield NaN, zero divisors yield Infinity","Validate numeric form input with Number.isFinite before it reaches bind parameters","When this fires, log the raw parameter values to find which computation produced NaN/Infinity"],"tags":["bind-parameters","nan","validation","javascript"],"backgroundTag":"invalid-bind-parameter","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}