{"record":{"id":"de62dbd982cca8e4","repo":"cayleygraph/cayley","slug":"error-checking-quad-validity-v","errorCode":null,"errorMessage":"error checking quad validity: %v","messagePattern":"error checking quad validity: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/nosql/quadstore.go","lineNumber":301,"sourceCode":"\tif err != nil {\n\t\terr = fmt.Errorf(\"quad update failed: %v\", err)\n\t}\n\treturn err\n}\n\nfunc checkQuadValid(q nosql.Document) bool {\n\tadded, _ := asInt(q[fldQuadAdded])\n\tdeleted, _ := asInt(q[fldQuadDeleted])\n\treturn added > deleted\n}\n\nfunc (qs *QuadStore) checkValidQuad(ctx context.Context, key nosql.Key) (bool, error) {\n\tq, err := qs.db.FindByKey(ctx, colQuads, key)\n\tif err == nosql.ErrNotFound {\n\t\treturn false, nil\n\t}\n\tif err != nil {\n\t\terr = fmt.Errorf(\"error checking quad validity: %v\", err)\n\t\treturn false, err\n\t}\n\treturn checkQuadValid(q), nil\n}\n\nfunc (qs *QuadStore) batchInsert(col string) nosql.DocWriter {\n\treturn nosql.BatchInsert(qs.db, col)\n}\n\nfunc (qs *QuadStore) appendLog(ctx context.Context, deltas []graph.Delta) ([]nosql.Key, error) {\n\tw := qs.batchInsert(colLog)\n\tdefer w.Close()\n\tfor _, d := range deltas {\n\t\tdata, err := proto.Marshal(pquads.MakeQuad(d.Quad))\n\t\tif err != nil {\n\t\t\treturn w.Keys(), err\n\t\t}\n\t\tvar action string","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/nosql/quadstore.go#L283-L319","documentation":"Returned by checkValidQuad when FindByKey on the quads collection returns an error other than ErrNotFound. A not-found quad is handled gracefully (returns false, nil), so this error signals a genuine database failure while reading a quad document during delta validation in ApplyDeltas.","triggerScenarios":"ApplyDeltas validating a quad removal when the backing store read fails: connection dropped, ctx cancelled, authentication failure, or a corrupted key/document in the quads collection.","commonSituations":"Expired MongoDB credentials mid-run; network partition during ApplyDeltas; backend returning unexpected errors for missing collections (older driver versions that don't map missing collection to ErrNotFound).","solutions":["Check the wrapped driver error and fix the underlying read failure (connectivity, auth).","Ensure the backend/driver maps missing documents/collections to nosql.ErrNotFound; upgrade the driver integration if not.","Verify database credentials and that the quads collection exists.","Retry ApplyDeltas after restoring connectivity; the read is side-effect free so retrying is safe.","Use a context with adequate timeout for large delta batches."],"exampleFix":"// before\nq, err := qs.db.FindByKey(ctx, colQuads, key)\nif err == nosql.ErrNotFound {\n    return false, nil\n}\n// after\n// caller-side guard: pre-check connectivity and retry reads\nif err != nil {\n    if isTransientDBErr(err) { // e.g. mongo.CommandError with network scope\n        return qs.checkValidQuad(ctx, key) // safe: read-only\n    }\n    return false, err\n}","handlingStrategy":"retry","validationCode":"// read path: verify backend health before ApplyDeltas validation\nif err := dbPing(ctx, spec); err != nil {\n    return fmt.Errorf(\"cannot validate quads, DB unreadable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"ok, err := validQuad(key) // via ApplyDeltas path\nif err != nil && isTransient(err) {\n    ok, err = retry(3, func() (bool, error) { return validQuad(key) }) // read is side-effect free\n}","preventionTips":["Refresh DB credentials before long-running jobs","Treat reads as safe to retry","Confirm the driver maps missing docs to nosql.ErrNotFound (upgrade otherwise)","Monitor for network partitions during ApplyDeltas"],"tags":["database","nosql","read-failed","cayley"],"backgroundTag":"database-query-failed","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}