Skip to content

Commit 917ccf9

Browse files
committed
scripting: return error when trying to suspend or stop a non-running vm
Fixes #4884
1 parent bd5de65 commit 917ccf9

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Scripting/UTMScriptingVirtualMachineImpl.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class UTMScriptingVirtualMachineImpl: NSObject {
143143
@objc func suspend(_ command: NSScriptCommand) {
144144
let shouldSaveState = command.evaluatedArguments?["saveFlag"] as? Bool ?? false
145145
withScriptCommand(command) { [self] in
146+
guard vm.state == .vmStarted else {
147+
throw ScriptingError.notRunning
148+
}
146149
try await vm.vmPause(save: shouldSaveState)
147150
}
148151
}
@@ -155,6 +158,9 @@ class UTMScriptingVirtualMachineImpl: NSObject {
155158
stopMethod = .force
156159
}
157160
withScriptCommand(command) { [self] in
161+
guard vm.state == .vmStarted || stopMethod == .kill else {
162+
throw ScriptingError.notRunning
163+
}
158164
switch stopMethod {
159165
case .force:
160166
try await vm.vmStop(force: false)
@@ -171,11 +177,13 @@ extension UTMScriptingVirtualMachineImpl {
171177
enum ScriptingError: Error, LocalizedError {
172178
case operationNotAvailable
173179
case operationNotSupported
180+
case notRunning
174181

175182
var errorDescription: String? {
176183
switch self {
177184
case .operationNotAvailable: return NSLocalizedString("Operation not available.", comment: "UTMScriptingVirtualMachineImpl")
178185
case .operationNotSupported: return NSLocalizedString("Operation not supported by the backend.", comment: "UTMScriptingVirtualMachineImpl")
186+
case .notRunning: return NSLocalizedString("The virtual machine is not running.", comment: "UTMScriptingVirtualMachineImpl")
179187
}
180188
}
181189
}

0 commit comments

Comments
 (0)