FuelLabs/fuels-ts · error · FuelError

ENCODE_ERROR

ENCODE_ERROR

Error message

Invalid boolean value.

What it means

Thrown by BooleanCoder.encode when value is neither strictly true nor strictly false (value === true || value === false is false). The coder deliberately rejects truthy/falsy values to avoid silent miscoding. Message: "Invalid boolean value.".

Source

Thrown at packages/abi-coder/src/encoding/coders/BooleanCoder.ts:27

export class BooleanCoder extends Coder<boolean, boolean> {
  options: EncodingOptions;

  constructor(
    options: EncodingOptions = {
      padToWordSize: false,
    }
  ) {
    const encodedLength = options.padToWordSize ? WORD_SIZE : 1;
    super('boolean', 'boolean', encodedLength);

    this.options = options;
  }

  encode(value: boolean): Uint8Array {
    const isTrueBool = value === true || value === false;

    if (!isTrueBool) {
      throw new FuelError(ErrorCode.ENCODE_ERROR, `Invalid boolean value.`);
    }

    return toBytes(value ? 1 : 0, this.encodedLength);
  }

  decode(data: Uint8Array, offset: number): [boolean, number] {
    if (data.length < this.encodedLength) {
      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid boolean data size.`);
    }

    const bytes = bn(data.slice(offset, offset + this.encodedLength));

    if (bytes.isZero()) {
      return [false, offset + this.encodedLength];
    }

    if (!bytes.eq(bn(1))) {
      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid boolean value.`);

View on GitHub (pinned to b3f37c91ac)

Solutions

  1. Coerce explicitly to a real boolean: Boolean(value) or value === true.
  2. Map 0/1 and 'true'/'false' strings to booleans at the trust boundary before encoding.
  3. Tighten the source type so only boolean reaches the coder.

Example fix

// before
bool.encode(rawFlag) // rawFlag is 0/1 or 'true'

// after
bool.encode(Boolean(rawFlag))
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value !== 'boolean') throw new Error('expected a real boolean')
bool.encode(value)

Type guard

const isStrictBoolean = (v: unknown): v is boolean => v === true || v === false

Try / catch

try { bool.encode(value as boolean) }
catch (e) { if ((e as FuelError).code === ErrorCode.ENCODE_ERROR) { /* coerce with Boolean(value) */ } throw e }

Prevention

When it happens

Trigger: Calling bool.encode(value) where value is e.g. 0/1, the strings 'true'/'false', undefined, null, a truthy object, or any non-boolean. TypeScript types usually prevent this, but it fires at runtime for any-typed or JSON-parsed input.

Common situations: API/JSON input parsed loosely; a number flag (0/1) passed where a boolean is expected; a 'true' string from an env var or query param; values coming from a loosely-typed config or form field.

Related errors


AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12). Data as JSON: /api/errors/a8d423201445f861. Report an issue: GitHub.