Skip to content

Commit

Permalink
scripting: return error when trying to suspend or stop a non-running vm
Browse files Browse the repository at this point in the history
Fixes #4884
  • Loading branch information
osy committed Jan 4, 2023
1 parent bd5de65 commit 917ccf9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Scripting/UTMScriptingVirtualMachineImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
Expand All @@ -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")
}
}
}
Expand Down

0 comments on commit 917ccf9

Please sign in to comment.