BornToBeRoot/NETworkManager · error · Exception

Wrong time unit.

Error message

Wrong time unit.

What it means

AutoRefreshTime.CalculateTimeSpan converts a time unit and value into a TimeSpan. Only Second/Minute/Hour cases are handled; TimeUnit.None or any unhandled unit falls into the default branch which throws Exception('Wrong time unit.'), because no meaningful interval can be computed.

Solutions

  1. Check that the TimeUnit is not None before calling CalculateTimeSpan and skip scheduling when auto-refresh is disabled
  2. Handle TimeUnit.None at the call site (disable the timer instead of computing a TimeSpan)
  3. Extend the switch in CalculateTimeSpan if a new TimeUnit member was added
  4. Catch the exception at the caller and fall back to the previous interval

Example fix

// before
var interval = AutoRefreshTime.CalculateTimeSpan(_autoRefreshTime); // throws when None
// after
var interval = _autoRefreshTime.TimeUnit == TimeUnit.None
    ? Timeout.InfiniteTimeSpan
    : AutoRefreshTime.CalculateTimeSpan(_autoRefreshTime);
Defensive patterns

Strategy: validation

Validate before calling

if (info.TimeUnit == TimeUnit.None)
    return; // auto-refresh disabled; do not compute an interval

Type guard

bool HasValidTimeUnit(AutoRefreshTime t) => t.TimeUnit is TimeUnit.Second or TimeUnit.Minute or TimeUnit.Hour;

Try / catch

try { interval = AutoRefreshTime.CalculateTimeSpan(info); }
catch (Exception ex) when (ex.Message == "Wrong time unit.") { interval = Timeout.InfiniteTimeSpan; }

Prevention

When it happens

Trigger: Calling CalculateTimeSpan with info whose TimeUnit is TimeUnit.None (the explicit case) — typically when the auto-refresh feature is disabled (None) but the interval is still calculated.

Common situations: Auto-refresh toggle set to 'Off' (None) while a refresh timer tries to compute the next interval; adding a new TimeUnit enum member without extending the switch; deserializing a settings value of 0/None.

Related errors


AI-assisted analysis of BornToBeRoot/NETworkManager@2780d65469 (2026-09-12). Data as JSON: /api/errors/745936a75730edf0. Report an issue: GitHub.

Appendix: source

Thrown at Source/NETworkManager.Utilities/AutoRefreshTime.cs:43

    /// </summary>
    /// <param name="info"><see cref="AutoRefreshTimeInfo" /> to calculate <see cref="TimeSpan" /></param>
    /// <returns>Returns the calculated <see cref="TimeSpan" />.</returns>
    public static TimeSpan CalculateTimeSpan(AutoRefreshTimeInfo info)
    {
        switch (info.TimeUnit)
        {
            // Calculate the seconds
            case TimeUnit.Second:
                return new TimeSpan(0, 0, info.Value);
            // Calculate the minutes
            case TimeUnit.Minute:
                return new TimeSpan(0, info.Value * 60, 0);
            // Calculate the hours
            case TimeUnit.Hour:
                return new TimeSpan(info.Value * 60, 0, 0);
            case TimeUnit.None:
            default:
                throw new Exception("Wrong time unit.");
        }
    }
}

View on GitHub (pinned to 2780d65469)