{"record":{"id":"b49afc1ac80b2641","repo":"TheAlgorithms/Java","slug":"invalid-unit-supported-units-are","errorCode":null,"errorMessage":"Invalid unit '{}'. Supported units are: {}","messagePattern":"Invalid unit '(.+?)'\\. Supported units are: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/TimeConverter.java","lineNumber":93,"sourceCode":"            throw new IllegalArgumentException(\"timeValue must be a non-negative number.\");\n        }\n\n        TimeUnit from = resolveUnit(unitFrom);\n        TimeUnit to = resolveUnit(unitTo);\n\n        double secondsValue = from.toSeconds(timeValue);\n        double converted = to.fromSeconds(secondsValue);\n\n        return Math.round(converted * 1000.0) / 1000.0;\n    }\n\n    private static TimeUnit resolveUnit(String unit) {\n        if (unit == null) {\n            throw new IllegalArgumentException(\"Unit cannot be null.\");\n        }\n        TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));\n        if (resolved == null) {\n            throw new IllegalArgumentException(\"Invalid unit '\" + unit + \"'. Supported units are: \" + UNIT_LOOKUP.keySet());\n        }\n        return resolved;\n    }\n}\n","sourceCodeStart":75,"sourceCodeEnd":98,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/TimeConverter.java#L75-L98","documentation":"Thrown by TimeConverter.convertTime when either unitFrom or unitTo does not match one of the seven supported units (seconds, minutes, hours, days, weeks, months, years). Lookup is case-insensitive (lowercased via Locale.ROOT) but the string must match exactly after lowercasing. The message interpolates the offending unit and prints the full supported set.","triggerScenarios":"Calling convertTime(value, \"minutes\", \"miliseconds\") (typo), passing a singular form like \"second\" instead of \"seconds\", passing abbreviations like \"min\"/\"hr\", or passing a non-time unit like \"celsius\".","commonSituations":"Integrating with upstream systems that emit unit abbreviations or singular forms; copy-paste errors; locale-specific unit names; assuming singular forms are accepted.","solutions":["Use exact plural forms from the supported set: seconds, minutes, hours, days, weeks, months, years.","Normalize the unit string before calling: trim, lowercase, and map abbreviations to canonical names.","If you need an unsupported unit, extend the enum and UNIT_LOOKUP map or wrap the value with manual conversion."],"exampleFix":"// before\ndouble r = TimeConverter.convertTime(5, \"min\", \"sec\");\n// after\nMap<String,String> aliases = Map.of(\"min\",\"minutes\",\"sec\",\"seconds\",\"hr\",\"hours\",\"day\",\"days\");\nString from = aliases.getOrDefault(unitFrom.trim().toLowerCase(), unitFrom.trim().toLowerCase());\ndouble r = TimeConverter.convertTime(5, \"minutes\", \"seconds\");","handlingStrategy":"validation","validationCode":"private static final Set<String> TIME_UNITS = Set.of(\"seconds\",\"minutes\",\"hours\",\"days\",\"weeks\",\"months\",\"years\");\nstatic String normalizeTimeUnit(String u) {\n    String n = u == null ? null : u.trim().toLowerCase(Locale.ROOT);\n    if (!TIME_UNITS.contains(n)) throw new IllegalArgumentException(\"Unsupported time unit: \" + u);\n    return n;\n}\n// before convertTime:\nString from = normalizeTimeUnit(unitFrom);\nString to = normalizeTimeUnit(unitTo);","typeGuard":"static boolean isValidTimeUnit(String unit) {\n    return unit != null && Set.of(\"seconds\",\"minutes\",\"hours\",\"days\",\"weeks\",\"weeks\",\"months\",\"years\")\n        .contains(unit.trim().toLowerCase(Locale.ROOT));\n}","tryCatchPattern":"try {\n    return TimeConverter.convertTime(value, unitFrom, unitTo);\n} catch (IllegalArgumentException e) {\n    // log supported units, fall back or rethrow with user-facing message\n    throw new ApiException(400, \"Unsupported time unit. Supported: seconds, minutes, hours, days, weeks, months, years\");\n}","preventionTips":["Maintain a canonical unit constant set and validate against it at input boundaries.","Map common abbreviations to canonical forms before calling convertTime.","Always use plural forms; never assume singular or abbreviated forms are accepted."],"tags":["java","time-conversion","input-validation","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}