{"record":{"id":"a517ac5d22b57db8","repo":"router-for-me/CLIProxyAPI","slug":"invalid-auth-file-w","errorCode":null,"errorMessage":"invalid auth file: %w","messagePattern":"invalid auth file: %w","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"internal/api/handlers/management/auth_files_crud.go","lineNumber":477,"sourceCode":"\t\treturn err\n\t}\n\treturn h.upsertAuthRecord(ctx, auth)\n}\n\nfunc (h *Handler) buildAuthFromFileData(path string, data []byte) (*coreauth.Auth, error) {\n\tif path == \"\" {\n\t\treturn nil, fmt.Errorf(\"auth path is empty\")\n\t}\n\tif data == nil {\n\t\tvar err error\n\t\tdata, err = os.ReadFile(path)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to read auth file: %w\", err)\n\t\t}\n\t}\n\tmetadata := make(map[string]any)\n\tif err := json.Unmarshal(data, &metadata); err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid auth file: %w\", err)\n\t}\n\tprovider, _ := metadata[\"type\"].(string)\n\tif provider == \"\" {\n\t\tprovider = \"unknown\"\n\t}\n\tlabel := provider\n\tif email, ok := metadata[\"email\"].(string); ok && email != \"\" {\n\t\tlabel = email\n\t}\n\tlastRefresh, hasLastRefresh := extractLastRefreshTimestamp(metadata)\n\n\tauthID := h.authIDForPath(path)\n\tif authID == \"\" {\n\t\tauthID = path\n\t}\n\tauth := (*coreauth.Auth)(nil)\n\tif h != nil && h.cfg != nil {\n\t\tsctx := &synthesizer.SynthesisContext{","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/api/handlers/management/auth_files_crud.go#L459-L495","documentation":"Thrown by buildAuthFromFileData when the auth file's bytes are not valid JSON (json.Unmarshal into map[string]any fails). The handler first parses the file generically to extract provider/type, email, and refresh metadata before provider-specific synthesis, so any syntax error aborts record building with the JSON decoder's cause wrapped in %w.","triggerScenarios":"Importing or scanning an auth file that is truncated, contains a BOM or trailing garbage, was hand-edited with a trailing comma or comment, or is actually YAML/TOML saved with a .json extension; endpoints that call buildAuthFromFileData with data==nil on such a file.","commonSituations":"Manual editing of auths/*.json to tweak a token or label; a partially written file after a crash during save; copy-pasting OAuth JSON from docs including markdown fences; mixing config.yaml syntax into an auth file.","solutions":["Validate the file with an external parser: jq . auths/<file> — the exact offset/line of the syntax error appears in the wrapped message","Fix the reported syntax error (remove comments, trailing commas, fences) or regenerate the file via the provider's OAuth login flow","Ensure the file is complete: re-export or re-run the login command if it was truncated","Retry the management import/list call after the file parses cleanly"],"exampleFix":"// before (auths/qwen.json)\n{ \"type\": \"qwen\", \"token\": \"...\", } // trailing comma\n// after\n{ \"type\": \"qwen\", \"token\": \"...\" }","handlingStrategy":"validation","validationCode":"if data, err := os.ReadFile(path); err == nil {\n    if !json.Valid(data) {\n        return fmt.Errorf(\"%s is not valid JSON; fix before import\", path)\n    }\n}","typeGuard":"func isValidAuthJSON(data []byte) bool { return json.Valid(bytes.TrimSpace(data)) }","tryCatchPattern":"if _, err := buildAuthFromFileData(path, data); err != nil {\n    if strings.HasPrefix(err.Error(), \"invalid auth file\") {\n        // quarantine the file; report the JSON offset from the wrapped cause\n    }\n}","preventionTips":["jq-validate any hand-edited auth file before restart","Modify auth files only through management endpoints or login commands","Never mix config.yaml syntax into auth JSON"],"tags":["json","auth","parsing","management-api"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}