FuelLabs/fuels-ts · error · FuelError
DECODE_ERROR
DECODE_ERROR
Error message
Invalid string slice data size.
What it means
Thrown by StrSliceCoder.decode() when the data buffer is shorter than WORD_SIZE (8 bytes). A Sway str slice is encoded as a u64 length prefix followed by UTF-8 bytes, requiring at least 8 bytes for the length prefix.
Source
Thrown at packages/abi-coder/src/encoding/coders/StrSliceCoder.ts:25
import { Coder } from './AbstractCoder';
import { BigNumberCoder } from './BigNumberCoder';
export class StrSliceCoder extends Coder<string, string> {
static memorySize = 1;
constructor() {
super('strSlice', 'str', WORD_SIZE);
}
encode(value: string): Uint8Array {
const bytes = toUtf8Bytes(value);
const lengthBytes = new BigNumberCoder('u64').encode(value.length);
return new Uint8Array([...lengthBytes, ...bytes]);
}
decode(data: Uint8Array, offset: number): [string, number] {
if (data.length < this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string slice data size.`);
}
const offsetAndLength = offset + WORD_SIZE;
const lengthBytes = data.slice(offset, offsetAndLength);
const length = bn(new BigNumberCoder('u64').decode(lengthBytes, 0)[0]).toNumber();
const bytes = data.slice(offsetAndLength, offsetAndLength + length);
if (bytes.length !== length) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string slice byte data size.`);
}
return [toUtf8String(bytes), offsetAndLength + length];
}
}
View on GitHub (pinned to b3f37c91ac)
Solutions
- Verify data.length >= 8 before calling decode().
- Confirm the data is a str slice encoding, not a std String or fixed-length str[N].
- Re-fetch data if truncation is suspected.
Example fix
// before
strSliceCoder.decode(new Uint8Array([0]), 0); // throws DECODE_ERROR
// after
if (data.length < 8) {
throw new Error('Data too short for str slice decode');
}
strSliceCoder.decode(data, 0); Defensive patterns
Strategy: validation
Validate before calling
function canDecodeStrSlice(data: Uint8Array): boolean {
return data.length >= 8; // WORD_SIZE
} Prevention
- Know the difference: str slice (StrSliceCoder), std String (StdStringCoder), fixed str[N] (StringCoder).
- Validate buffer size before decoding variable-length string types.
- Log the Sway type string when debugging decode failures.
When it happens
Trigger: Passing fewer than 8 bytes to decode(). Decoding data not produced by StrSliceCoder. Receiving a truncated ABI payload from an RPC response.
Common situations: Confusing str slice (str) with std String (StdStringCoder) or fixed str[N] (StringCoder). Malformed RPC data. Buffer truncation in transit.
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/94d15ab40ef1828a.
Report an issue: GitHub.