{"record":{"id":"75a805d6c9d59432","repo":"router-for-me/CLIProxyAPI","slug":"token-store-unavailable","errorCode":null,"errorMessage":"token store unavailable","messagePattern":"token store unavailable","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"internal/api/handlers/management/auth_files_fields.go","lineNumber":676,"sourceCode":"\nfunc cleanAuthFilePath(path string) string {\n\tpath = strings.TrimSpace(path)\n\tif path == \"\" {\n\t\treturn \"\"\n\t}\n\tif abs, errAbs := filepath.Abs(path); errAbs == nil && strings.TrimSpace(abs) != \"\" {\n\t\tpath = abs\n\t}\n\treturn filepath.Clean(path)\n}\n\nfunc (h *Handler) deleteTokenRecord(ctx context.Context, path string) error {\n\tif strings.TrimSpace(path) == \"\" {\n\t\treturn fmt.Errorf(\"auth path is empty\")\n\t}\n\tstore := h.tokenStoreWithBaseDir()\n\tif store == nil {\n\t\treturn fmt.Errorf(\"token store unavailable\")\n\t}\n\treturn store.Delete(ctx, path)\n}\n\nfunc (h *Handler) tokenStoreWithBaseDir() coreauth.Store {\n\tif h == nil {\n\t\treturn nil\n\t}\n\tstore := h.tokenStore\n\tif store == nil {\n\t\tstore = sdkAuth.GetTokenStore()\n\t\th.tokenStore = store\n\t}\n\tif h.cfg != nil {\n\t\tif dirSetter, ok := store.(interface{ SetBaseDir(string) }); ok {\n\t\t\tdirSetter.SetBaseDir(h.cfg.AuthDir)\n\t\t}\n\t}","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/api/handlers/management/auth_files_fields.go#L658-L694","documentation":"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.","triggerScenarios":"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.","commonSituations":"Embedding only the management API without the cliproxy service; partial refactors of handler wiring; test harnesses hitting endpoints directly.","solutions":["Ensure the SDK service (which registers the global token store) is started before management endpoints serve requests","If constructing the Handler manually, inject a token store via its wiring/constructor","In tests, provide a fake store so tokenStoreWithBaseDir returns non-nil","Retry the delete once the service is fully initialized"],"exampleFix":"// before\nh := &management.Handler{} // no store, SDK not started\n// after\nsvc := cliproxy.NewService(cfg) // registers the global token store\nh := management.NewHandler(svc)","handlingStrategy":"type-guard","validationCode":"if h.tokenStoreWithBaseDir() == nil {\n    return errors.New(\"token store not initialized — start the SDK service first\")\n}","typeGuard":"func (h *Handler) hasTokenStore() bool { return h != nil && h.tokenStoreWithBaseDir() != nil }","tryCatchPattern":null,"preventionTips":["Start the SDK service before mounting management routes","Inject a token store explicitly when embedding","Assert store availability in smoke tests of management endpoints"],"tags":["initialization","dependency-injection","token-store"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}