{"record":{"id":"1ee3b9b8dcd086a5","repo":"FuelLabs/fuels-ts","slug":"encode-error-1ee3b9","errorCode":"ENCODE_ERROR","errorMessage":"Value length mismatch during encode.","messagePattern":"Value length mismatch during encode\\.","errorType":"exception","errorClass":"FuelError","httpStatus":null,"severity":"error","filePath":"packages/abi-coder/src/encoding/coders/StringCoder.ts","lineNumber":13,"sourceCode":"import { ErrorCode, FuelError } from '@fuel-ts/errors';\nimport { toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';\n\nimport { Coder } from './AbstractCoder';\n\nexport class StringCoder<TLength extends number = number> extends Coder<string, string> {\n  constructor(length: TLength) {\n    super('string', `str[${length}]`, length);\n  }\n\n  encode(value: string): Uint8Array {\n    if (value.length !== this.encodedLength) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Value length mismatch during encode.`);\n    }\n\n    return toUtf8Bytes(value);\n  }\n\n  decode(data: Uint8Array, offset: number): [string, number] {\n    if (data.length < this.encodedLength) {\n      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string 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 string byte data size.`);\n    }\n\n    return [toUtf8String(bytes), offset + this.encodedLength];\n  }","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/FuelLabs/fuels-ts/blob/b3f37c91aca4aa9d5e4c0d3967f66237190826ea/packages/abi-coder/src/encoding/coders/StringCoder.ts#L1-L31","documentation":"Thrown by StringCoder.encode() when the input string's character length does not exactly match the coder's declared fixed length N (from str[N] in the ABI). StringCoder represents Sway's fixed-size string type; the character count must be exactly N. This uses JavaScript string .length (UTF-16 code units), so strings with surrogate pairs may behave unexpectedly.","triggerScenarios":"ABI declares str[5] but you pass 'hi' (2 chars) or 'hello world' (11 chars). Passing an empty string to str[10]. Passing user input of a different length than the fixed field expects.","commonSituations":"ABI mismatch: the Sway contract declares a different str length than the client assumes. User input that is shorter or longer than the fixed field. Forgetting to pad fixed-length strings to their declared size.","solutions":["Pad or truncate the string to exactly N characters before encoding.","Update the ABI type to match the actual string length, or use a variable-length type.","Use a variable-length type (StdString/StrSlice) if the length is dynamic."],"exampleFix":"// before\nconst coder = new StringCoder(5);\ncoder.encode('hi'); // throws ENCODE_ERROR\n\n// after\nconst coder = new StringCoder(5);\nconst padded = 'hi'.padEnd(5, '\\0');\ncoder.encode(padded);","handlingStrategy":"validation","validationCode":"function fitsFixedString(value: string, length: number): boolean {\n  return value.length === length;\n}","typeGuard":"function isFixedStringLength(value: unknown, length: number): value is string {\n  return typeof value === 'string' && value.length === length;\n}","tryCatchPattern":null,"preventionTips":["Pad fixed-length strings to the exact ABI-declared length before encoding.","Validate user input length against the ABI schema at the application boundary.","Prefer str slice or std String for variable-length text."],"tags":["abi-coder","encoding","string","fixed-length"],"backgroundTag":null,"analyzedSha":"b3f37c91aca4aa9d5e4c0d3967f66237190826ea","analyzedAt":"2026-08-12T20:30:56.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}