{"record":{"id":"34783cdf4757dfdf","repo":"toeverything/AFFiNE","slug":"transformererror-34783c","errorCode":"TransformerError","errorMessage":"Root block not found in doc","messagePattern":"Root block not found in doc","errorType":"exception","errorClass":"BlockSuiteError","httpStatus":null,"severity":"error","filePath":"blocksuite/framework/store/src/transformer/transformer.ts","lineNumber":109,"sourceCode":"\n      return snapshot;\n    } catch (error) {\n      console.error(`Error when transforming block to snapshot:`);\n      console.error(error);\n      return;\n    }\n  };\n\n  docToSnapshot = (doc: Store): DocSnapshot | undefined => {\n    try {\n      this._slots.beforeExport.next({\n        type: 'page',\n        page: doc,\n      });\n      const rootModel = doc.root;\n      const meta = this._exportDocMeta(doc);\n      if (!rootModel) {\n        throw new BlockSuiteError(\n          ErrorCode.TransformerError,\n          'Root block not found in doc'\n        );\n      }\n      const blocks = this.blockToSnapshot(toDraftModel(rootModel));\n      if (!blocks) {\n        return;\n      }\n      const docSnapshot: DocSnapshot = {\n        type: 'page',\n        meta,\n        blocks,\n      };\n      this._slots.afterExport.next({\n        type: 'page',\n        page: doc,\n        snapshot: docSnapshot,\n      });","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/26c515e050211269e911f7d9cfe162a26c83ed98/blocksuite/framework/store/src/transformer/transformer.ts#L91-L127","documentation":"Thrown by Transformer.docToSnapshot() when doc.root is undefined — i.e. the Store (doc) has no root block model. Exporting a doc to a snapshot requires traversing the block tree starting from the root; without a root, the export cannot proceed. Note: the outer docToSnapshot catches this and returns undefined, so callers see undefined rather than the raw error.","triggerScenarios":"Calling transformer.docToSnapshot(doc) on a Store that was created but never had a root block added, or whose root block was removed. Also possible if doc.load() wasn't called or the Yjs doc has no root structure.","commonSituations":"Exporting a newly created doc before adding any blocks. Doc corruption where the root block was deleted. Creating a Store via docCRUD.create(id) but forgetting to add a root block and load the doc before export.","solutions":["Ensure a root block (e.g. 'affine:page') is added to the doc before calling docToSnapshot.","Check doc.root for undefined before attempting export and handle the empty-doc case.","Call doc.load() after creating the Store to ensure models are initialized from the CRDT state."],"exampleFix":"// before\nconst doc = docCRUD.create('doc-id');\ndoc.load();\ntransformer.docToSnapshot(doc); // no root -> returns undefined\n\n// after\nconst doc = docCRUD.create('doc-id');\ndoc.load();\ndoc.addBlock('affine:page', {});\ntransformer.docToSnapshot(doc);","handlingStrategy":"validation","validationCode":"if (!doc.root) {\n  console.warn('Doc has no root block; cannot export to snapshot.');\n  return;\n}\nconst snapshot = transformer.docToSnapshot(doc);","typeGuard":"const hasRootBlock = (doc: Store): boolean => doc.root !== undefined && doc.root !== null;","tryCatchPattern":"const snapshot = transformer.docToSnapshot(doc);\nif (!snapshot) {\n  // docToSnapshot catches internally and returns undefined\n  console.warn('Export returned undefined — doc may have no root block.');\n}","preventionTips":["Ensure a root block is added before attempting export.","Check doc.root before calling docToSnapshot.","Call doc.load() after creating a Store to initialize models."],"tags":["blocksuite","transformer","snapshot","export","root-block"],"backgroundTag":null,"analyzedSha":"26c515e050211269e911f7d9cfe162a26c83ed98","analyzedAt":"2026-08-12T13:15:16.447Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}