code: func (b *Local) backupStateForError(stateFile *statefile.File, err error) error {
  	b.CLI.Error(fmt.Sprintf("Failed to save state: %s\n", err))
  
  	local := statemgr.NewFilesystem("errored.tfstate")
  	writeErr := local.WriteStateForMigration(stateFile, true)
  	if writeErr != nil {
  		b.CLI.Error(fmt.Sprintf(
  			"Also failed to create local state file for recovery: %s\n\n", writeErr,
  		))
  		// To avoid leaving the user with no state at all, our last resort
  		// is to print the JSON state out onto the terminal. This is an awful
  		// UX, so we should definitely avoid doing this if at all possible,
  		// but at least the user has _some_ path to recover if we end up
  		// here for some reason.
  		stateBuf := new(bytes.Buffer)
  		jsonErr := statefile.Write(stateFile, stateBuf)
  		if jsonErr != nil {
  			b.CLI.Error(fmt.Sprintf(
  				"Also failed to JSON-serialize the state to print it: %s\n\n", jsonErr,
  			))
  			return errors.New(stateWriteFatalError)
  		}
  
  		b.CLI.Output(stateBuf.String())
  
  		return errors.New(stateWriteConsoleFallbackError)
  	}
  
  	return errors.New(stateWriteBackedUpError)
  }
docs: // backupStateForError is called in a scenario where we're unable to persist the
  // state for some reason, and will attempt to save a backup copy of the state
  // to local disk to help the user recover. This is a "last ditch effort" sort
  // of thing, so we really don't want to end up in this codepath; we should do
  // everything we possibly can to get the state saved _somewhere_.