pinpoint-apm/pinpoint · error · Error

Invalid time unit provided.

Error message

Invalid time unit provided.

What it means

convertToMilliseconds converts a time value with a unit ('s','m','h','d','w','y') into milliseconds. When the unit string does not match any known case, the switch falls through to default and the function throws 'Invalid time unit provided.' to signal an unrecognized TimeUnitFormat.

Source

Thrown at web-frontend/src/main/v3/packages/datetime-picker/src/utils/date.ts:66

        case 's':
          return timeNumber * 1000;
        case 'm':
          return timeNumber * 60 * 1000;
        case 'h':
          return timeNumber * 60 * 60 * 1000;
        case 'd':
          return timeNumber * 24 * 60 * 60 * 1000;
        case 'w':
          return timeNumber * 7 * 24 * 60 * 60 * 1000;
        case 'mo':
          return timeNumber * 30 * 24 * 60 * 60 * 1000;
        case 'y':
          return timeNumber * 365 * 24 * 60 * 60 * 1000;
        default:
          break;
      }
    }
    throw new Error('Invalid time unit provided.');
  }
};

export const convertToTimeUnit = (milliseconds = 0): TimeUnitFormat => {
  const seconds = Math.ceil(milliseconds / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);
  const weeks = Math.floor(days / 7);
  const months = Math.floor(days / 30);
  const years = Math.floor(months / 12);

  if (years >= 1) {
    return `${years}y`;
  } else if (months >= 1) {
    return `${months}mo`;
  } else if (weeks >= 1) {
    return `${weeks}w`;

View on GitHub (pinned to 744c3d3075)

Solutions

  1. Check the unit value against the supported set ('s','m','h','d','w','y') before calling
  2. Normalize/trim/lowercase the input unit string
  3. Add the missing unit case to the switch if the new unit is legitimately needed
  4. Add a validation warning in dev so unknown units are caught early

Example fix

// before
convertToMilliseconds(10, 'M');
// after
convertToMilliseconds(10, 'm'); // or validate first:
const units = ['s','m','h','d','w','y'];
if (!units.includes(unit)) throw new Error(`Unit must be one of ${units.join(',')}`);
Defensive patterns

Strategy: validation

Validate before calling

const SUPPORTED = ['s','m','h','d','w','y'];
if (!SUPPORTED.includes(unit)) throw new RangeError(`unit must be one of ${SUPPORTED.join(',')}, got ${unit}`);

Type guard

const isTimeUnit = (u: unknown): u is 's'|'m'|'h'|'d'|'w'|'y' =>
  typeof u === 'string' && ['s','m','h','d','w','y'].includes(u);

Try / catch

try { ms = convertToMilliseconds(value, unit); } catch (e) { if (e.message === 'Invalid time unit provided.') { ms = fallbackMs; } else throw e; }

Prevention

When it happens

Trigger: Calling convertToMilliseconds with a unit string outside the supported set, e.g. convertToMilliseconds(5, 'M') or 'sec', or with an empty/undefined unit that fails the inner lookup, hitting the default branch.

Common situations: Passing a capitalized unit like 'Y' or 'H', using 'mo' for months (not supported), units arriving from user input or config files without normalization, or refactoring a legacy time-unit constant that used full words.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of pinpoint-apm/pinpoint@744c3d3075 (2026-09-07). Data as JSON: /api/errors/b991c6fe3b87d266. Report an issue: GitHub.