{"record":{"id":"824e7a9ad2a44273","repo":"gitbutlerapp/gitbutler","slug":"expected-to-be-in-edit-mode","errorCode":null,"errorMessage":"Expected to be in edit mode","messagePattern":"Expected to be in edit mode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-operating-modes/src/lib.rs","lineNumber":223,"sourceCode":"    Ok(operating_mode(ctx, perm)? == OperatingMode::OpenWorkspace)\n}\n\npub fn ensure_open_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<()> {\n    if in_open_workspace_mode(ctx, perm)? {\n        Ok(())\n    } else {\n        bail!(\"Expected to be in open workspace mode\")\n    }\n}\n\npub fn in_edit_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {\n    Ok(matches!(operating_mode(ctx, perm)?, OperatingMode::Edit(_)))\n}\n\npub fn ensure_edit_mode(ctx: &Context, perm: &RepoShared) -> Result<EditModeMetadata> {\n    match operating_mode(ctx, perm)? {\n        OperatingMode::Edit(edit_mode_metadata) => Ok(edit_mode_metadata),\n        _ => Err(anyhow!(\"Expected to be in edit mode\")),\n    }\n}\n\npub fn in_outside_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {\n    Ok(matches!(\n        operating_mode(ctx, perm)?,\n        OperatingMode::OutsideWorkspace(_)\n    ))\n}\n","sourceCodeStart":205,"sourceCodeEnd":233,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-operating-modes/src/lib.rs#L205-L233","documentation":"ensure_edit_mode returns the EditModeMetadata only when the repository's current operating mode is Edit; any other mode (outside-workspace, review, etc.) yields this error. It guards operations that only make sense while editing a workspace.","triggerScenarios":"Calling save_and_return_to_workspace, abort_and_return_to_workspace, or starting_index_state when the repo is not in edit mode — e.g. before entering edit mode, after it was exited by another client/process, or after the mode marker was cleaned up.","commonSituations":"Two GitButler clients or a CLI and app both touching the same repo, one exiting edit mode; stale app state after a crash removed the mode marker; calling the API without first entering edit mode.","solutions":["Enter edit mode before performing the operation (the mode check precedes these calls).","Check operating_mode() to see which mode the repo is actually in.","If another process changed the mode, refresh/resync the app state.","Handle the error gracefully — the operation is invalid in the current mode, not retryable as-is."],"exampleFix":"// before\nlet meta = ensure_edit_mode(&ctx, &perm)?; // fails if not editing\n// after\nif !in_edit_mode(&ctx, &perm)? {\n    enter_edit_mode(&ctx, &perm)?;\n}\nlet meta = ensure_edit_mode(&ctx, &perm)?;","handlingStrategy":"validation","validationCode":"if !in_edit_mode(&ctx, &perm)? {\n    // enter edit mode first or skip the operation\n    return Err(anyhow!(\"operation requires edit mode\"));\n}","typeGuard":null,"tryCatchPattern":"match ensure_edit_mode(&ctx, &perm) {\n    Err(e) if e.to_string() == \"Expected to be in edit mode\" => {\n        // recover: refresh mode state or re-enter edit mode\n    }\n    r => r?,\n}","preventionTips":["Always check in_edit_mode/operating_mode before mode-specific operations.","Avoid multiple clients mutating the same repo's operating mode concurrently.","Refresh app state after crashes that may have cleared mode markers."],"tags":["operating-modes","state-machine","gitbutler"],"backgroundTag":"invalid-state-transition","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}