FuelLabs/fuels-ts · error · FuelError

INVALID_POLICY_TYPE

INVALID_POLICY_TYPE

Error message

Invalid policy type: ${type}

What it means

Thrown by PolicyCoder.encode() when a policy's type is not one of Tip(1), WitnessLimit(2), Maturity(4), MaxFee(8), Expiration(16), or Owner(32). The encode switch falls to default. It indicates a policy constructed with an out-of-range or unsupported type value.

Source

Thrown at packages/transactions/src/coders/policy.ts:109

    sortedPolicies.forEach(({ data, type }) => {
      switch (type) {
        case PolicyType.MaxFee:
        case PolicyType.Tip:
        case PolicyType.WitnessLimit:
          parts.push(new BigNumberCoder('u64').encode(data));
          break;

        case PolicyType.Maturity:
        case PolicyType.Expiration:
          parts.push(new NumberCoder('u32', { padToWordSize: true }).encode(data));
          break;
        case PolicyType.Owner:
          parts.push(new BigNumberCoder('u64').encode(data));
          break;

        default: {
          throw new FuelError(ErrorCode.INVALID_POLICY_TYPE, `Invalid policy type: ${type}`);
        }
      }
    });

    return concat(parts);
  }

  decode(data: Uint8Array, offset: number, policyTypes: number): [Policy[], number] {
    let o = offset;
    const policies: Policy[] = [];
    const policyTypesArray = getPolicyTypesArray(policyTypes);

    for (const policyType of policyTypesArray) {
      switch (policyType) {
        case PolicyType.Tip: {
          const [tip, nextOffset] = new BigNumberCoder('u64').decode(data, o);
          o = nextOffset;
          policies.push({ type: PolicyType.Tip, data: tip });

View on GitHub (pinned to b3f37c91ac)

Solutions

  1. Import and use the PolicyType enum for policy.type.
  2. Validate policy.type is one of the known PolicyType members before encoding.
  3. Upgrade @fuel-ts/transactions if a newer policy type is required.

Example fix

// before
const policy = { type: 64, data: bn(1) };

// after
import { PolicyType } from '@fuel-ts/transactions';
const policy = { type: PolicyType.MaxFee, data: bn(1) };
Defensive patterns

Strategy: type-guard

Validate before calling

import { PolicyType } from '@fuel-ts/transactions';

const VALID_POLICY_TYPES = new Set<PolicyType>([
  PolicyType.Tip, PolicyType.WitnessLimit, PolicyType.Maturity,
  PolicyType.MaxFee, PolicyType.Expiration, PolicyType.Owner,
]);
policies.forEach(p => { if (!VALID_POLICY_TYPES.has(p.type)) throw new Error(`Bad policy type ${p.type}`); });

Type guard

import { PolicyType } from '@fuel-ts/transactions';
function isValidPolicyType(t: number): boolean {
  return [PolicyType.Tip, PolicyType.WitnessLimit, PolicyType.Maturity, PolicyType.MaxFee, PolicyType.Expiration, PolicyType.Owner].includes(t as PolicyType);
}

Try / catch

try {
  policyCoder.encode(policies, policyTypes);
} catch (e) {
  if (e instanceof FuelError && e.code === ErrorCode.INVALID_POLICY_TYPE) {
    // replace the invalid policy.type with a valid PolicyType enum member
  } else throw e;
}

Prevention

When it happens

Trigger: Manually building a Policy object with type set to an invalid number; passing type 0 or values not in the PolicyType bitfield; version skew introducing an unknown policy type.

Common situations: Constructing policies from raw numbers instead of the PolicyType enum; using a type value reserved/added by a newer consensus version the SDK does not support.

Related errors


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