{"record":{"id":"4bdee3d4bbe52cfa","repo":"louislam/uptime-kuma","slug":"invalid-duration-duration","errorCode":null,"errorMessage":"Invalid duration: ${duration}","messagePattern":"Invalid duration: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"server/uptime-calculator.js","lineNumber":779,"sourceCode":"                default:\n                    throw new Error(\"Invalid type\");\n            }\n        }\n\n        return result;\n    }\n\n    /**\n     * Get the uptime data for given duration.\n     * @param {string} duration  A string with a number and a unit (m,h,d,w,M,y), such as 24h, 30d, 1y.\n     * @returns {UptimeDataResult} UptimeDataResult\n     * @throws {Error} Invalid duration / Unsupported unit\n     */\n    getDataByDuration(duration) {\n        const durationNumStr = duration.slice(0, -1);\n\n        if (!/^[0-9]+$/.test(durationNumStr)) {\n            throw new Error(`Invalid duration: ${duration}`);\n        }\n        const num = Number(durationNumStr);\n        const unit = duration.slice(-1);\n\n        switch (unit) {\n            case \"m\":\n                return this.getData(num, \"minute\");\n            case \"h\":\n                return this.getData(num, \"hour\");\n            case \"d\":\n                return this.getData(num, \"day\");\n            case \"w\":\n                return this.getData(7 * num, \"day\");\n            case \"M\":\n                return this.getData(30 * num, \"day\");\n            case \"y\":\n                return this.getData(365 * num, \"day\");\n            default:","sourceCodeStart":761,"sourceCodeEnd":797,"githubUrl":"https://github.com/louislam/uptime-kuma/blob/6b5ea0155793e666666745fb8d6fef1e829543a2/server/uptime-calculator.js#L761-L797","documentation":"Thrown by UptimeCalculator.getDataByDuration(duration) when `duration.slice(0, -1)` (everything except the last character) does not match `^[0-9]+$`. The parser expects exactly a positive integer literal followed by a single unit letter. Any non-digit characters in the numeric portion, an empty numeric portion, decimals, or a leading sign are rejected.","triggerScenarios":"Calling getDataByDuration with: \"abc\" (numeric portion \"ab\"), \"h\" (numeric portion \"\"), \"1.5h\" (\"1.5\" fails), \"-3d\" (\"-3\" fails), \"\" (slicing empty string), \" 24h\" (leading space), \"24 h\" (space before unit). Note api-router.js:245-247 normalizes a bare integer like \"24\" to \"24h\" first, so a pure-number badge duration does NOT reach this error; only malformed strings do.","commonSituations":"Badge URL with a malformed duration segment (`/api/badge/:id/status/abc` → \"abc\" slice \"ab\" fails); user-typed duration with a decimal (\"0.5h\"); trailing whitespace or BOM in the duration; copy-paste of a duration with the wrong unit convention.","solutions":["Normalize the duration to `<integer><unit>` (e.g. \"24h\", \"30d\", \"1y\") before calling getDataByDuration.","Strip whitespace and reject decimals/signs upstream: parse with `/^(\\d+)([mhdwMy])$/`.","If you accept bare numbers, append the default unit yourself (as api-router does for hours).","Return a 400 from badge routes when the duration does not match the canonical pattern."],"exampleFix":"// before\nconst up = uptimeCalculator.getDataByDuration(\"1.5h\"); // -> \"1.5\" fails /^[0-9]+$/\n\n// after\nconst m = /^(\\d+)([mhdwMy])$/.exec(String(duration).trim());\nif (!m) throw new Error(`Bad duration: ${duration}`);\nconst up = uptimeCalculator.getDataByDuration(m[1] + m[2]);","handlingStrategy":"validation","validationCode":"const m = /^(\\d+)([mhdwMy])$/.exec(String(duration).trim());\nif (!m) throw new Error(\"Duration must be <int><unit>\");\ngetDataByDuration(m[1] + m[2]);","typeGuard":"const isCanonicalDuration = (v) => typeof v === \"string\" && /^\\d+[mhdwMy]$/.test(v.trim());","tryCatchPattern":"try { getDataByDuration(d); } catch (e) { if (/Invalid duration/.test(e.message)) { /* normalize then retry */ } throw e; }","preventionTips":["Strip whitespace and validate with /^\\d+[mhdwMy]$/ before calling.","Append the default unit to bare integers yourself.","Reject decimals and signs upstream."],"tags":["uptime-calculator","duration","validation","badge","uptime-kuma"],"backgroundTag":null,"analyzedSha":"6b5ea0155793e666666745fb8d6fef1e829543a2","analyzedAt":"2026-08-12T23:42:12.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}