router-for-me/CLIProxyAPI · error

invalid auth file: %w

Error message

invalid auth file: %w

What it means

While flipping the disabled flag on a plugin source auth file, json.Unmarshal of the existing (non-empty) file into map[string]any failed. Unlike a hard read failure, the file was read fine but its content is not parseable JSON, so the handler refuses to rewrite it rather than destroying the malformed content.

Source

Thrown at internal/api/handlers/management/auth_files_fields.go:166

			return fmt.Errorf("failed to update auth %s: %w", auth.ID, errUpdate)
		}
	}
	return nil
}

func setSourceAuthFileDisabled(path string, disabled bool) error {
	path = strings.TrimSpace(path)
	if path == "" {
		return fmt.Errorf("source auth path is empty")
	}
	data, errRead := os.ReadFile(path)
	if errRead != nil {
		return errRead
	}
	metadata := make(map[string]any)
	if len(bytes.TrimSpace(data)) > 0 {
		if errUnmarshal := json.Unmarshal(data, &metadata); errUnmarshal != nil {
			return fmt.Errorf("invalid auth file: %w", errUnmarshal)
		}
	}
	if metadata == nil {
		metadata = make(map[string]any)
	}
	metadata["disabled"] = disabled
	raw, errMarshal := json.Marshal(metadata)
	if errMarshal != nil {
		return fmt.Errorf("marshal auth file: %w", errMarshal)
	}
	if errWrite := os.WriteFile(path, raw, 0o600); errWrite != nil {
		return errWrite
	}
	return nil
}

func applyAuthDisabledState(auth *coreauth.Auth, disabled bool) {
	if auth == nil {

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Run jq . on the file to get the exact syntax error location from the wrapped message
  2. Repair or regenerate the source file (re-run the plugin login/import)
  3. Back up the file first if the token inside is still needed — the operation aborts without modifying it
  4. Retry the toggle after the file parses

Example fix

// before: auths/source.json contains {"token": "abc",}
// after: fix to {"token": "abc"} then retry the PATCH
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(path)
if err == nil && len(bytes.TrimSpace(data)) > 0 && !json.Valid(data) {
    return fmt.Errorf("refusing to patch %s: not valid JSON — repair first", path)
}

Type guard

func isParsableAuthFile(data []byte) bool {
    t := bytes.TrimSpace(data)
    return len(t) == 0 || json.Valid(t)
}

Try / catch

if err := setSourceAuthFileDisabled(path, true); err != nil && strings.HasPrefix(err.Error(), "invalid auth file") {
    // abort safely: file untouched; repair then retry
}

Prevention

When it happens

Trigger: Toggling disabled on a source file that was hand-edited into invalid JSON, truncated by a crash, or written by an external tool in another format; the pre-write parse guard in setSourceAuthFileDisabled catches it before metadata["disabled"] could be merged.

Common situations: Manual token refresh edits to plugin source files; files synced from another system with encoding issues (BOM, corrupted escapes); partial writes from a killed process.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/6f3e5f76fc53eddd. Report an issue: GitHub.