{"record":{"id":"09c551353432740f","repo":"kataras/iris","slug":"invalid-duration-09c551","errorCode":null,"errorMessage":"invalid duration","messagePattern":"invalid duration","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/jsonx/time_notation.go","lineNumber":87,"sourceCode":"\t}\n\n\tvar v any\n\tif err := json.Unmarshal(b, &v); err != nil {\n\t\treturn err\n\t}\n\tswitch value := v.(type) {\n\tcase float64:\n\t\t*d = TimeNotationDuration(value)\n\t\treturn nil\n\tcase string:\n\t\tdv, err := ParseTimeNotationDuration(value)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t*d = dv\n\t\treturn nil\n\tdefault:\n\t\treturn errors.New(\"invalid duration\")\n\t}\n}\n\nfunc (d TimeNotationDuration) ToDuration() time.Duration {\n\treturn time.Duration(d)\n}\n\nfunc (d TimeNotationDuration) Value() (driver.Value, error) {\n\treturn d.ToDuration(), nil\n}\n\n// Set sets the value of duration in nanoseconds.\nfunc (d *TimeNotationDuration) Set(v float64) {\n\tif math.IsNaN(v) {\n\t\treturn\n\t}\n\n\t*d = TimeNotationDuration(v)","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/x/jsonx/time_notation.go#L69-L105","documentation":"TimeNotationDuration.UnmarshalJSON returns 'invalid duration' when the JSON token is neither a number nor a recognized duration string with time notation (e.g. \"5s\", \"2h30m\"). Only number and string cases are handled; all other JSON kinds hit the default error branch.","triggerScenarios":"Unmarshalling {\"interval\": [1,2]} or {\"interval\": true} into a TimeNotationDuration field; a string that the duration parser fails to convert.","commonSituations":"Config files where a duration field was given as a list or object; unit mistakes such as \"5 seconds\" (space, wrong unit) instead of \"5s\".","solutions":["Provide the value as a number or a valid Go duration string like \"300ms\" or \"1h\"","Correct the JSON field's type in config/payload","Pre-validate the config with a schema or encoding/json dry-run"],"exampleFix":"// before\n{\"interval\": \"5 minutes\"}\n// after\n{\"interval\": \"5m\"}","handlingStrategy":"validation","validationCode":"var probe map[string]json.RawMessage\nif err := json.Unmarshal(raw, &probe); err != nil { return err }\nif v, ok := probe[\"interval\"]; ok && v[0] != '\"' && !(v[0] >= '0' && v[0] <= '9') {\n    return fmt.Errorf(\"interval must be number or duration string like \\\"5s\\\"\")\n}","typeGuard":"func isInvalidTimeNotation(err error) bool { return err != nil && strings.Contains(err.Error(), \"invalid duration\") }","tryCatchPattern":"if err := json.Unmarshal(raw, &cfg); err != nil {\n    if strings.Contains(err.Error(), \"invalid duration\") {\n        return fmt.Errorf(\"use Go duration notation, e.g. \\\"2h30m\\\"\")\n    }\n    return err\n}","preventionTips":["Use Go duration string syntax (\"300ms\", \"1h\") in configs, never \"5 minutes\"","Validate configs at startup with a dry-run decode","Keep duration fields flat scalars, never objects/arrays"],"tags":["json","duration","unmarshal"],"backgroundTag":"json-unmarshal-type-mismatch","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}