FuelLabs/fuels-ts · error · FuelError
DECODE_ERROR
DECODE_ERROR
Error message
Invalid string data size.
What it means
Thrown by StringCoder.decode() when the data buffer is shorter than the fixed length N declared by the coder. Unlike variable-length string coders, StringCoder expects exactly N bytes of data to decode a fixed-length Sway str[N].
Source
Thrown at packages/abi-coder/src/encoding/coders/StringCoder.ts:21
import { Coder } from './AbstractCoder';
export class StringCoder<TLength extends number = number> extends Coder<string, string> {
constructor(length: TLength) {
super('string', `str[${length}]`, length);
}
encode(value: string): Uint8Array {
if (value.length !== this.encodedLength) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Value length mismatch during encode.`);
}
return toUtf8Bytes(value);
}
decode(data: Uint8Array, offset: number): [string, number] {
if (data.length < this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string data size.`);
}
const bytes = data.slice(offset, offset + this.encodedLength);
if (bytes.length !== this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string byte data size.`);
}
return [toUtf8String(bytes), offset + this.encodedLength];
}
}
View on GitHub (pinned to b3f37c91ac)
Solutions
- Verify data.length >= coder.encodedLength (N) before calling decode().
- Confirm the coder's N matches the ABI's str[N] declaration.
- Ensure data is not truncated in transit.
Example fix
// before
new StringCoder(10).decode(new Uint8Array([65, 66]), 0); // throws DECODE_ERROR
// after
const coder = new StringCoder(10);
if (data.length < coder.encodedLength) {
throw new Error(`Need ${coder.encodedLength} bytes, got ${data.length}`);
}
coder.decode(data, 0); Defensive patterns
Strategy: validation
Validate before calling
function canDecodeFixedString(data: Uint8Array, length: number): boolean {
return data.length >= length;
} Prevention
- Validate buffer length against the ABI str[N] before decoding.
- Ensure encoder and decoder use the same ABI.
- Log coder.encodedLength on decode failures.
When it happens
Trigger: Decoding a buffer of 2 bytes when the coder expects str[10] (10 bytes). Truncated ABI payload. Using the wrong coder for the data format.
Common situations: ABI mismatch between encoder and decoder. Truncated network data. Wrong coder type used (fixed-length vs variable-length string).
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/abf4c3ebe7a909bf.
Report an issue: GitHub.