router-for-me/CLIProxyAPI · error

token store unavailable

Error message

token store unavailable

What it means

deleteTokenRecord found no usable token store: tokenStoreWithBaseDir returned nil because the handler is nil or both the injected h.tokenStore and the SDK-global sdkAuth.GetTokenStore() are nil. The latter means the embedded SDK's token-store global was never initialized in this process, so persistence operations cannot proceed.

Source

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

func cleanAuthFilePath(path string) string {
	path = strings.TrimSpace(path)
	if path == "" {
		return ""
	}
	if abs, errAbs := filepath.Abs(path); errAbs == nil && strings.TrimSpace(abs) != "" {
		path = abs
	}
	return filepath.Clean(path)
}

func (h *Handler) deleteTokenRecord(ctx context.Context, path string) error {
	if strings.TrimSpace(path) == "" {
		return fmt.Errorf("auth path is empty")
	}
	store := h.tokenStoreWithBaseDir()
	if store == nil {
		return fmt.Errorf("token store unavailable")
	}
	return store.Delete(ctx, path)
}

func (h *Handler) tokenStoreWithBaseDir() coreauth.Store {
	if h == nil {
		return nil
	}
	store := h.tokenStore
	if store == nil {
		store = sdkAuth.GetTokenStore()
		h.tokenStore = store
	}
	if h.cfg != nil {
		if dirSetter, ok := store.(interface{ SetBaseDir(string) }); ok {
			dirSetter.SetBaseDir(h.cfg.AuthDir)
		}
	}

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Ensure the SDK service (which registers the global token store) is started before management endpoints serve requests
  2. If constructing the Handler manually, inject a token store via its wiring/constructor
  3. In tests, provide a fake store so tokenStoreWithBaseDir returns non-nil
  4. Retry the delete once the service is fully initialized

Example fix

// before
h := &management.Handler{} // no store, SDK not started
// after
svc := cliproxy.NewService(cfg) // registers the global token store
h := management.NewHandler(svc)
Defensive patterns

Strategy: type-guard

Validate before calling

if h.tokenStoreWithBaseDir() == nil {
    return errors.New("token store not initialized — start the SDK service first")
}

Type guard

func (h *Handler) hasTokenStore() bool { return h != nil && h.tokenStoreWithBaseDir() != nil }

Prevention

When it happens

Trigger: Invoking a delete endpoint in a process where the SDK service was never started (management routes mounted standalone); unit tests constructing a Handler without injecting a token store; initialization-order bugs where the route runs before SDK bootstrap sets the global store.

Common situations: Embedding only the management API without the cliproxy service; partial refactors of handler wiring; test harnesses hitting endpoints directly.

Related errors


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