Tencent/WeKnora · error
failed to get tenant: %w
Error message
failed to get tenant: %w
What it means
GetChatHistoryKBStats needs the tenant record to read its ChatHistoryConfig. It calls tenantService.GetTenantByID with the tenant ID from context; any failure fetching the tenant is wrapped here, so stats cannot be computed.
Source
Thrown at internal/application/service/message.go:449
return
}
if len(knowledgeIDs) == 0 {
return
}
logger.Infof(ctx, "Deleting %d chat history knowledge entries for session %s", len(knowledgeIDs), sessionID)
if err := s.knowService.DeleteKnowledgeList(ctx, knowledgeIDs); err != nil {
logger.Warnf(ctx, "Failed to batch delete chat history knowledge for session %s: %v", sessionID, err)
}
}
// GetChatHistoryKBStats returns statistics about the chat history knowledge base.
func (s *messageService) GetChatHistoryKBStats(ctx context.Context) (*types.ChatHistoryKBStats, error) {
tenantID := types.MustTenantIDFromContext(ctx)
tenant, err := s.tenantService.GetTenantByID(ctx, tenantID)
if err != nil {
return nil, fmt.Errorf("failed to get tenant: %w", err)
}
stats := &types.ChatHistoryKBStats{}
cfg := tenant.ChatHistoryConfig
if cfg == nil || !cfg.Enabled {
return stats, nil
}
stats.Enabled = true
stats.EmbeddingModelID = cfg.EmbeddingModelID
stats.KnowledgeBaseID = cfg.KnowledgeBaseID
if cfg.KnowledgeBaseID == "" {
return stats, nil
}
// Fetch KB info and fill counts (KnowledgeCount is gorm:"-", needs FillKnowledgeBaseCounts)
kb, err := s.kbService.GetKnowledgeBaseByID(ctx, cfg.KnowledgeBaseID)View on GitHub (pinned to 988cbb0330)
Solutions
- Check the wrapped error from GetTenantByID for the root cause (not found vs connection failure).
- Verify types.MustTenantIDFromContext(ctx) yields a valid, existing tenant ID.
- Confirm the tenant exists in the database and was not soft-deleted.
- If not-found, re-authenticate so the context carries a current tenant ID.
Example fix
// before
tenantID := types.MustTenantIDFromContext(ctx)
tenant, err := s.tenantService.GetTenantByID(ctx, tenantID)
if err != nil {
return nil, fmt.Errorf("failed to get tenant: %w", err)
}
// after
tenantID, err := types.TenantIDFromContext(ctx)
if err != nil {
return nil, fmt.Errorf("missing tenant in context: %w", err)
}
tenant, err := s.tenantService.GetTenantByID(ctx, tenantID)
if err != nil {
return nil, fmt.Errorf("failed to get tenant: %w", err)
} Defensive patterns
Strategy: type-guard
Validate before calling
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok || tenantID == 0 { return fmt.Errorf("no tenant in context") } Type guard
func hasTenant(ctx context.Context) bool {
id, err := types.TenantIDFromContext(ctx)
return err == nil && id != 0
} Try / catch
stats, err := svc.GetChatHistoryKBStats(ctx)
if err != nil && strings.Contains(err.Error(), "failed to get tenant") {
// tenant missing/deleted: force re-auth or return 404 to client
} Prevention
- Always set the tenant ID in middleware before service calls
- Soft-delete checks: verify tenant exists before issuing tokens carrying its ID
- Cache tenant lookups to reduce dependence on tenant DB availability
When it happens
Trigger: Calling GetChatHistoryKBStats with a context whose tenant ID does not exist, has been deleted, or when the tenant service/repository errors (DB down, timeout, permission issue).
Common situations: Stale tenant ID in auth context after tenant deletion; cross-tenant request where the tenant row was never created; tenant service DB outage.
Related errors
- failed to ensure FAQ knowledge: %w
- failed to create chunk: %w
- failed to update chunk status: %w
- failed to list FAQ chunks: %w
- failed to build tag map: %w
AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02).
Data as JSON: /api/errors/83565edc175575ef.
Report an issue: GitHub.