{"record":{"id":"ace1eaa4c75bb3e9","repo":"FuelLabs/fuels-ts","slug":"encode-error-ace1ea","errorCode":"ENCODE_ERROR","errorMessage":"Invalid ${this.baseType}.","messagePattern":"Invalid (.+?)\\.","errorType":"exception","errorClass":"FuelError","httpStatus":null,"severity":"error","filePath":"packages/abi-coder/src/encoding/coders/NumberCoder.ts","lineNumber":46,"sourceCode":"  constructor(\n    baseType: NumberCoderType,\n    options: EncodingOptions = {\n      padToWordSize: false,\n    }\n  ) {\n    const length = options.padToWordSize ? WORD_SIZE : getLength(baseType);\n    super('number', baseType, length);\n    this.baseType = baseType;\n    this.options = options;\n  }\n\n  encode(value: number | string): Uint8Array {\n    let bytes;\n\n    try {\n      bytes = toBytes(value);\n    } catch (error) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.baseType}.`);\n    }\n\n    if (bytes.length > this.encodedLength) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid ${this.baseType}, too many bytes.`);\n    }\n\n    return toBytes(bytes, this.encodedLength);\n  }\n\n  decode(data: Uint8Array, offset: number): [number, number] {\n    if (data.length < this.encodedLength) {\n      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid number data size.`);\n    }\n\n    const bytes = data.slice(offset, offset + this.encodedLength);\n\n    if (bytes.length !== this.encodedLength) {\n      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid number byte data size.`);","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/FuelLabs/fuels-ts/blob/b3f37c91aca4aa9d5e4c0d3967f66237190826ea/packages/abi-coder/src/encoding/coders/NumberCoder.ts#L28-L64","documentation":"Thrown by NumberCoder.encode() when the underlying toBytes()/bn() call fails to convert the input value into a byte array. NumberCoder encodes Sway unsigned integers (u8, u16, u32); if the value is not a valid numeric representation, the conversion throws and this ENCODE_ERROR wraps it. The message includes the baseType (e.g. 'u8') to identify which integer coder rejected the value.","triggerScenarios":"Calling NumberCoder('u8').encode(undefined), passing NaN from a failed parseInt, providing a non-numeric string like 'abc', or passing an object. Also triggered indirectly when encoding a struct or tuple whose numeric field is undefined and the field coder is a NumberCoder.","commonSituations":"An optional struct field is left undefined but the ABI type is a bare u32 (not Option<u32>). A value arrives from JSON as a string containing non-numeric characters. Arithmetic produces NaN that flows into encode().","solutions":["Ensure the value is a finite number or a valid numeric/hex string before calling encode().","If the field is genuinely nullable, declare it as Option<T> in the Sway ABI so OptionCoder is used instead of NumberCoder.","Validate with Number.isFinite() or a BN conversion check at the application boundary before encoding."],"exampleFix":"// before\nconst coder = new NumberCoder('u32');\ncoder.encode(maybeUndefined); // throws ENCODE_ERROR\n\n// after\nconst coder = new NumberCoder('u32');\nconst val = maybeUndefined ?? 0;\nif (!Number.isFinite(val)) throw new Error('Expected a finite number');\ncoder.encode(val);","handlingStrategy":"validation","validationCode":"function isValidNumberInput(value: unknown): boolean {\n  if (typeof value === 'number') return Number.isFinite(value);\n  if (typeof value === 'string') {\n    const trimmed = value.trim();\n    if (trimmed === '') return false;\n    return !isNaN(Number(trimmed));\n  }\n  return false;\n}","typeGuard":"function isNumberInput(value: unknown): value is number | string {\n  if (typeof value === 'number') return Number.isFinite(value);\n  if (typeof value === 'string') {\n    const t = value.trim();\n    return t !== '' && !isNaN(Number(t));\n  }\n  return false;\n}","tryCatchPattern":"try {\n  const encoded = numberCoder.encode(value);\n} catch (e) {\n  if (e instanceof FuelError && e.code === ErrorCode.ENCODE_ERROR) {\n    // value was not a valid number; log and provide default\n  }\n  throw e;\n}","preventionTips":["Default optional number fields to 0 instead of leaving them undefined.","Use Option<T> in the Sway ABI for genuinely nullable numeric fields.","Validate numeric inputs at the application boundary before ABI encoding."],"tags":["abi-coder","encoding","number","validation"],"backgroundTag":null,"analyzedSha":"b3f37c91aca4aa9d5e4c0d3967f66237190826ea","analyzedAt":"2026-08-12T20:30:56.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}