netchx/netch · error · MessageException

Unknown Mode Type

Error message

Unknown Mode Type

What it means

GetModeControllerByType maps a ModeType to its IModeController: ProcessMode->NFController, TunMode->TUNController, ShareMode->PcapController. The default arm is a defensive guard - the ModeType enum currently defines only those three members, so reaching default requires a future enum member added without a case, or an invalid/out-of-range ModeType arriving from deserialization. It logs the numeric value then throws MessageException.

Source

Thrown at Netch/Services/ModeService.cs:113

        if (File.Exists(mode.FullName))
            File.Delete(mode.FullName);
    }

    public static IModeController GetModeControllerByType(ModeType type, out ushort? port, out string portName)
    {
        port = null;
        portName = string.Empty;
        switch (type)
        {
            case ModeType.ProcessMode:
                return new NFController();
            case ModeType.TunMode:
                return new TUNController();
            case ModeType.ShareMode:
                return new PcapController();
            default:
                Log.Error("Unknown Mode Type \"{Type}\"", (int)type);
                throw new MessageException("Unknown Mode Type");
        }
    }
}

View on GitHub (pinned to 9d99eb1c5a)

Solutions

  1. Add a case for the new ModeType returning its controller (e.g. case ModeType.NewMode: return new NewController();).
  2. If using a custom mode, ensure its ModeType is one of ProcessMode/TunMode/ShareMode.
  3. Validate the deserialized ModeType with Enum.IsDefined before calling this method.
  4. Convert the switch to an exhaustive expression switch so the compiler flags a missing case when a new member is added.

Example fix

// before
default:
    Log.Error("Unknown Mode Type \"{Type}\"", (int)type);
    throw new MessageException("Unknown Mode Type");
// after - exhaustive expression switch with compiler enforcement
return type switch
{
    ModeType.ProcessMode => new NFController(),
    ModeType.TunMode     => new TUNController(),
    ModeType.ShareMode   => new PcapController(),
    _ => throw new InvalidOperationException($"ModeType {(int)type} has no controller")
};
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(ModeType), type))
    throw new MessageException($"Invalid ModeType {type}");

Type guard

bool IsKnownModeType(ModeType t) => t is ModeType.ProcessMode or ModeType.TunMode or ModeType.ShareMode;

Try / catch

try { var ctrl = ModeService.GetModeControllerByType(type, out _, out _); }
catch (MessageException ex) when (ex.Message == "Unknown Mode Type")
{
    Log.Error("Cannot start: mode type {Type} unsupported in this build", type);
}

Prevention

When it happens

Trigger: A code change adds a new ModeType enum member but forgets to add a case here; deserialization of a mode JSON with a ModeType value outside the enum's defined range; an unsafe cast producing an invalid enum integer.

Common situations: Branch development adding a new mode type incompletely; corrupted mode file yielding a bad ModeType; essentially unreachable in a clean shipping build.

Related errors


AI-assisted analysis of netchx/netch@9d99eb1c5a (2026-08-13). Data as JSON: /api/errors/98de63c631aeba70. Report an issue: GitHub.