From 917ccf9c74b0b86c1523905113660f80ddb6c6ba Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Tue, 3 Jan 2023 18:32:08 -0800 Subject: [PATCH] scripting: return error when trying to suspend or stop a non-running vm Fixes #4884 --- Scripting/UTMScriptingVirtualMachineImpl.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Scripting/UTMScriptingVirtualMachineImpl.swift b/Scripting/UTMScriptingVirtualMachineImpl.swift index 51bddc59f..6996d175a 100644 --- a/Scripting/UTMScriptingVirtualMachineImpl.swift +++ b/Scripting/UTMScriptingVirtualMachineImpl.swift @@ -143,6 +143,9 @@ class UTMScriptingVirtualMachineImpl: NSObject { @objc func suspend(_ command: NSScriptCommand) { let shouldSaveState = command.evaluatedArguments?["saveFlag"] as? Bool ?? false withScriptCommand(command) { [self] in + guard vm.state == .vmStarted else { + throw ScriptingError.notRunning + } try await vm.vmPause(save: shouldSaveState) } } @@ -155,6 +158,9 @@ class UTMScriptingVirtualMachineImpl: NSObject { stopMethod = .force } withScriptCommand(command) { [self] in + guard vm.state == .vmStarted || stopMethod == .kill else { + throw ScriptingError.notRunning + } switch stopMethod { case .force: try await vm.vmStop(force: false) @@ -171,11 +177,13 @@ extension UTMScriptingVirtualMachineImpl { enum ScriptingError: Error, LocalizedError { case operationNotAvailable case operationNotSupported + case notRunning var errorDescription: String? { switch self { case .operationNotAvailable: return NSLocalizedString("Operation not available.", comment: "UTMScriptingVirtualMachineImpl") case .operationNotSupported: return NSLocalizedString("Operation not supported by the backend.", comment: "UTMScriptingVirtualMachineImpl") + case .notRunning: return NSLocalizedString("The virtual machine is not running.", comment: "UTMScriptingVirtualMachineImpl") } } }