{"record":{"id":"0b7ba1756d643a7f","repo":"router-for-me/CLIProxyAPI","slug":"metadata-is-nil","errorCode":null,"errorMessage":"metadata is nil","messagePattern":"metadata is nil","errorType":"validation","errorClass":null,"httpStatus":400,"severity":"warning","filePath":"internal/api/handlers/management/auth_files_fields.go","lineNumber":347,"sourceCode":"\t\treturn nil, err\n\t}\n\treturn value, nil\n}\n\nfunc rootAuthFileField(path string) string {\n\tpath = strings.TrimSpace(path)\n\tif path == \"\" {\n\t\treturn \"\"\n\t}\n\tif idx := strings.Index(path, \".\"); idx >= 0 {\n\t\treturn strings.TrimSpace(path[:idx])\n\t}\n\treturn path\n}\n\nfunc setAuthFileMetadataValue(metadata map[string]any, path string, value any) error {\n\tif metadata == nil {\n\t\treturn fmt.Errorf(\"metadata is nil\")\n\t}\n\tparts := strings.Split(path, \".\")\n\tcurrent := metadata\n\tfor i, rawPart := range parts {\n\t\tpart := strings.TrimSpace(rawPart)\n\t\tif part == \"\" {\n\t\t\treturn fmt.Errorf(\"invalid field path: %s\", path)\n\t\t}\n\t\tif i == len(parts)-1 {\n\t\t\tcurrent[part] = value\n\t\t\treturn nil\n\t\t}\n\t\tnext, ok := current[part].(map[string]any)\n\t\tif !ok {\n\t\t\tnext = make(map[string]any)\n\t\t\tcurrent[part] = next\n\t\t}\n\t\tcurrent = next","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/api/handlers/management/auth_files_fields.go#L329-L365","documentation":"setAuthFileMetadataValue was invoked with a nil metadata map, so there is nowhere to write the requested dotted-path field. Callers load metadata from an auth file beforehand; nil means that load step produced nothing (caller passed nil explicitly or the file was empty), and the helper treats it as an invalid-state error instead of silently allocating, leaving semantics to the caller.","triggerScenarios":"PATCH of an auth-file field where the source file was empty or blank so the caller's metadata map stayed nil, or where the caller skipped the load entirely; any code path reaching field-patch without a successfully parsed file backing it.","commonSituations":"Empty (zero-byte) auth files after a truncated save; test code invoking the setter with a nil map; refactors that reordered load-then-set logic.","solutions":["Check whether the target auth file is empty (wc -c) and regenerate it if truncated","Ensure the caller initializes metadata to an empty map when the file legitimately has no content before patching fields","Add a guard in calling code: if metadata == nil, initialize it or return a clearer file-level error","Retry the field PATCH after the file is valid JSON"],"exampleFix":"// before\nvar metadata map[string]any\nsetAuthFileMetadataValue(metadata, \"weight\", 2)\n// after\nmetadata := map[string]any{}\nif len(data) > 0 { json.Unmarshal(data, &metadata) }\nsetAuthFileMetadataValue(metadata, \"weight\", 2)","handlingStrategy":"validation","validationCode":"if metadata == nil {\n    metadata = map[string]any{} // or reject with a clearer file-level error\n}","typeGuard":"func hasMetadata(m map[string]any) bool { return m != nil }","tryCatchPattern":null,"preventionTips":["Always initialize the metadata map after reading a possibly-empty file","Detect zero-byte auth files at startup and quarantine them"],"tags":["validation","metadata","nil-guard"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}