{"record":{"id":"cca4941f0d5b50c2","repo":"vxcontrol/pentagi","slug":"knowledge-get-document-s-w","errorCode":null,"errorMessage":"knowledge: get document %s: %w","messagePattern":"knowledge: get document (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/database/knowledge/knowledge.go","lineNumber":314,"sourceCode":"\t} else {\n\t\trows, err := ks.db.ListUserKnowledgeDocuments(ctx, nsOf(userIDStr))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"knowledge: list user docs: %w\", err)\n\t\t}\n\t\tfor _, r := range rows {\n\t\t\tdocs = append(docs, rowToModel(r.ID, r.Document, nullStr(r.Cmetadata), withContent))\n\t\t}\n\t}\n\n\treturn applyGoFilters(docs, filter), nil\n}\n\n// ---- GetDocument (admin) ----------------------------------------------------\n\nfunc (ks *knowledgeStore) GetDocument(ctx context.Context, id string) (*model.KnowledgeDocument, error) {\n\trow, err := ks.db.GetKnowledgeDocument(ctx, id)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"knowledge: get document %s: %w\", id, err)\n\t}\n\treturn rowToModel(row.ID, row.Document, nullStr(row.Cmetadata), true), nil\n}\n\n// ---- GetUserDocument (user-scoped) ------------------------------------------\n\nfunc (ks *knowledgeStore) GetUserDocument(ctx context.Context, userID int64, id string) (*model.KnowledgeDocument, error) {\n\trow, err := ks.db.GetUserKnowledgeDocument(ctx, database.GetUserKnowledgeDocumentParams{\n\t\tUuid:   id,\n\t\tUserID: nsOf(strconv.FormatInt(userID, 10)),\n\t})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"knowledge: get user document %s: %w\", id, err)\n\t}\n\treturn rowToModel(row.ID, row.Document, nullStr(row.Cmetadata), true), nil\n}\n\n// ---- SearchDocuments (admin) ------------------------------------------------","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/database/knowledge/knowledge.go#L296-L332","documentation":"GetDocument fetches a single knowledge document by UUID with no user scoping (admin path). It calls the SQLC query GetKnowledgeDocument; a not-found row, DB error, or canceled context is wrapped as 'knowledge: get document <id>'.","triggerScenarios":"Calling GetDocument (directly or via UpdateDocument/RenameDocument/DeleteDocument) with a UUID that does not exist in the embedding table, or when the database is unavailable.","commonSituations":"Admin UI holds a stale document id after the row was deleted by another session; id typos; sql.ErrNoRows on a document deleted concurrently between list and get.","solutions":["Confirm the document UUID exists (query the embedding table or re-list documents).","Handle sql.ErrNoRows specially to return a clean not-found to the caller instead of a raw DB error.","Check DB connectivity if the wrapped error is connection-related.","Re-run goose migrations if the embedding table is missing (fresh database)."],"exampleFix":"// before\nrow, err := ks.db.GetKnowledgeDocument(ctx, id)\nif err != nil {\n    return nil, fmt.Errorf(\"knowledge: get document %s: %w\", id, err)\n}\n// after\nrow, err := ks.db.GetKnowledgeDocument(ctx, id)\nif errors.Is(err, sql.ErrNoRows) {\n    return nil, fmt.Errorf(\"knowledge: document %s not found\", id)\n}\nif err != nil {\n    return nil, fmt.Errorf(\"knowledge: get document %s: %w\", id, err)\n}","handlingStrategy":"type-guard","validationCode":"if _, err := uuid.Parse(id); err != nil {\n    return nil, fmt.Errorf(\"invalid document id: %w\", err)\n}","typeGuard":"func isNotFound(err error) bool {\n    return errors.Is(err, sql.ErrNoRows)\n}","tryCatchPattern":"doc, err := store.GetDocument(ctx, id)\nif err != nil {\n    if isNotFound(err) {\n        return ErrDocumentNotFound // map to 404\n    }\n    return fmt.Errorf(\"fetch document: %w\", err)\n}","preventionTips":["Refresh stale document ids in the UI after any delete operation.","Treat sql.ErrNoRows as a domain not-found error, not an internal error.","Validate UUID format before issuing queries.","Avoid long-lived caches of document ids."],"tags":["database","not-found","sqlc"],"backgroundTag":"record-not-found","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}