FuelLabs/fuels-ts · error · FuelError
ENCODE_ERROR
ENCODE_ERROR
Error message
Expected array value.
What it means
Thrown by RawSliceCoder.encode() when the input is not a JavaScript array. RawSliceCoder encodes Sway raw untyped slices and expects a number[] (array of byte values 0-255). Passing any non-array value, including a string, number, object, or even a Uint8Array, triggers this error because Uint8Array fails Array.isArray().
Source
Thrown at packages/abi-coder/src/encoding/coders/RawSliceCoder.ts:18
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { WORD_SIZE } from '../../utils/constants';
import { Coder } from './AbstractCoder';
import { ArrayCoder } from './ArrayCoder';
import { BigNumberCoder } from './BigNumberCoder';
import { NumberCoder } from './NumberCoder';
export class RawSliceCoder extends Coder<number[], number[]> {
constructor() {
super('raw untyped slice', 'raw untyped slice', WORD_SIZE);
}
encode(value: number[]): Uint8Array {
if (!Array.isArray(value)) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Expected array value.`);
}
const internalCoder = new ArrayCoder(new NumberCoder('u8'), value.length);
const bytes = internalCoder.encode(value);
const lengthBytes = new BigNumberCoder('u64').encode(bytes.length);
return new Uint8Array([...lengthBytes, ...bytes]);
}
decode(data: Uint8Array, offset: number): [number[], number] {
if (data.length < this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid raw 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 dataBytes = data.slice(offsetAndLength, offsetAndLength + length);View on GitHub (pinned to b3f37c91ac)
Solutions
- Convert the value to number[] before encoding: Array.from(uint8Array) or split a string into char codes.
- If you have a Uint8Array, use Array.from() to convert, or use VecCoder/BytesCoder which accepts Uint8Array directly.
- Ensure each element is an integer in the 0-255 range.
Example fix
// before
rawSliceCoder.encode('abc'); // throws ENCODE_ERROR
rawSliceCoder.encode(new Uint8Array([97])); // throws ENCODE_ERROR
// after
rawSliceCoder.encode([97, 98, 99]);
rawSliceCoder.encode(Array.from(uint8Array)); Defensive patterns
Strategy: type-guard
Validate before calling
function isRawSliceValue(value: unknown): boolean {
return Array.isArray(value) && value.every(v => typeof v === 'number' && v >= 0 && v <= 255 && Number.isInteger(v));
} Type guard
function isRawSliceInput(value: unknown): value is number[] {
return Array.isArray(value) && value.every(v => typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= 255);
} Prevention
- Distinguish RawSliceCoder (number[]) from VecCoder (number[] or Uint8Array).
- Use Array.from() to convert typed arrays before passing to RawSliceCoder.
- Validate that each byte is an integer in the 0-255 range.
When it happens
Trigger: Calling encode('hello') (string instead of char codes), encode(42), or encode(new Uint8Array([...])). Note that Uint8Array is not accepted by RawSliceCoder even though it looks array-like. Passing a BN or object also fails.
Common situations: Confusing RawSliceCoder (number[]) with VecCoder (which does accept Uint8Array). Passing a hex string instead of a byte array. Deserializing data from JSON as an object rather than an array.
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/b51a4b9bd9b22363.
Report an issue: GitHub.