diff --git a/errorcode_string.go b/errorcode_string.go new file mode 100644 index 0000000..08b3b3d --- /dev/null +++ b/errorcode_string.go @@ -0,0 +1,51 @@ +// Code generated by "stringer -type=ErrorCode"; DO NOT EDIT. + +package vz + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[ErrorInternal-1] + _ = x[ErrorInvalidVirtualMachineConfiguration-2] + _ = x[ErrorInvalidVirtualMachineState-3] + _ = x[ErrorInvalidVirtualMachineStateTransition-4] + _ = x[ErrorInvalidDiskImage-5] + _ = x[ErrorVirtualMachineLimitExceeded-6] + _ = x[ErrorNetworkError-7] + _ = x[ErrorOutOfDiskSpace-8] + _ = x[ErrorOperationCancelled-9] + _ = x[ErrorNotSupported-10] + _ = x[ErrorRestoreImageCatalogLoadFailed-10001] + _ = x[ErrorInvalidRestoreImageCatalog-10002] + _ = x[ErrorNoSupportedRestoreImagesInCatalog-10003] + _ = x[ErrorRestoreImageLoadFailed-10004] + _ = x[ErrorInvalidRestoreImage-10005] + _ = x[ErrorInstallationRequiresUpdate-10006] + _ = x[ErrorInstallationFailed-10007] +} + +const ( + _ErrorCode_name_0 = "ErrorInternalErrorInvalidVirtualMachineConfigurationErrorInvalidVirtualMachineStateErrorInvalidVirtualMachineStateTransitionErrorInvalidDiskImageErrorVirtualMachineLimitExceededErrorNetworkErrorErrorOutOfDiskSpaceErrorOperationCancelledErrorNotSupported" + _ErrorCode_name_1 = "ErrorRestoreImageCatalogLoadFailedErrorInvalidRestoreImageCatalogErrorNoSupportedRestoreImagesInCatalogErrorRestoreImageLoadFailedErrorInvalidRestoreImageErrorInstallationRequiresUpdateErrorInstallationFailed" +) + +var ( + _ErrorCode_index_0 = [...]uint8{0, 13, 52, 83, 124, 145, 177, 194, 213, 236, 253} + _ErrorCode_index_1 = [...]uint8{0, 34, 65, 103, 130, 154, 185, 208} +) + +func (i ErrorCode) String() string { + switch { + case 1 <= i && i <= 10: + i -= 1 + return _ErrorCode_name_0[_ErrorCode_index_0[i]:_ErrorCode_index_0[i+1]] + case 10001 <= i && i <= 10007: + i -= 10001 + return _ErrorCode_name_1[_ErrorCode_index_1[i]:_ErrorCode_index_1[i+1]] + default: + return "ErrorCode(" + strconv.FormatInt(int64(i), 10) + ")" + } +} diff --git a/vzerror.go b/vzerror.go new file mode 100644 index 0000000..8298269 --- /dev/null +++ b/vzerror.go @@ -0,0 +1,77 @@ +package vz + +// Error type returned by the Virtualization framework. +// The NSError domain is VZErrorDomain, the code is one of the ErrorCode constants. +// +//go:generate stringer -type=ErrorCode +type ErrorCode int + +const ( + // ErrorInternal is an internal error such as the virtual machine unexpectedly stopping. + ErrorInternal ErrorCode = 1 + iota + + // ErrorInvalidVirtualMachineConfiguration represents invalid machine configuration. + ErrorInvalidVirtualMachineConfiguration + + // ErrorInvalidVirtualMachineState represents API used with a machine in the wrong state + // (e.g. interacting with a machine before it is running). + ErrorInvalidVirtualMachineState + + // ErrorInvalidVirtualMachineStateTransition is invalid change of state + // (e.g. pausing a virtual machine that is not started). + ErrorInvalidVirtualMachineStateTransition + + // ErrorInvalidDiskImage represents unrecognized disk image format or invalid disk image. + ErrorInvalidDiskImage + + // ErrorVirtualMachineLimitExceeded represents the running virtual machine limit was exceeded. + // Available from macOS 12.0 and above. + ErrorVirtualMachineLimitExceeded + + // ErrorNetworkError represents network error occurred. + // Available from macOS 13.0 and above. + ErrorNetworkError + + // ErrorOutOfDiskSpace represents machine ran out of disk space. + // Available from macOS 13.0 and above. + ErrorOutOfDiskSpace + + // ErrorOperationCancelled represents the operation was cancelled. + // Available from macOS 13.0 and above. + ErrorOperationCancelled + + // ErrorNotSupported represents the operation is not supported. + // Available from macOS 13.0 and above. + ErrorNotSupported +) + +/* macOS installation errors. */ +const ( + // ErrorRestoreImageCatalogLoadFailed represents the restore image catalog failed to load. + // Available from macOS 13.0 and above. + ErrorRestoreImageCatalogLoadFailed ErrorCode = 10001 + iota + + // ErrorInvalidRestoreImageCatalog represents the restore image catalog is invalid. + // Available from macOS 13.0 and above. + ErrorInvalidRestoreImageCatalog + + // ErrorNoSupportedRestoreImagesInCatalog represents the restore image catalog has no supported restore images. + // Available from macOS 13.0 and above. + ErrorNoSupportedRestoreImagesInCatalog + + // ErrorRestoreImageLoadFailed represents the restore image failed to load. + // Available from macOS 13.0 and above. + ErrorRestoreImageLoadFailed + + // ErrorInvalidRestoreImage represents the restore image is invalid. + // Available from macOS 13.0 and above. + ErrorInvalidRestoreImage + + // ErrorInstallationRequiresUpdate represents a software update is required to complete the installation. + // Available from macOS 13.0 and above. + ErrorInstallationRequiresUpdate + + // ErrorInstallationFailed is an error occurred during installation. + // Available from macOS 13.0 and above. + ErrorInstallationFailed +) diff --git a/vzerror_test.go b/vzerror_test.go new file mode 100644 index 0000000..4acfad8 --- /dev/null +++ b/vzerror_test.go @@ -0,0 +1,106 @@ +package vz_test + +import ( + "testing" + + "github.com/Code-Hex/vz/v2" +) + +func TestErrorCodeString(t *testing.T) { + t.Run("fundamental error code", func(t *testing.T) { + cases := []struct { + code vz.ErrorCode + want string + }{ + { + code: vz.ErrorInternal, + want: "ErrorInternal", + }, + { + code: vz.ErrorInvalidVirtualMachineConfiguration, + want: "ErrorInvalidVirtualMachineConfiguration", + }, + { + code: vz.ErrorInvalidVirtualMachineState, + want: "ErrorInvalidVirtualMachineState", + }, + { + code: vz.ErrorInvalidVirtualMachineStateTransition, + want: "ErrorInvalidVirtualMachineStateTransition", + }, + { + code: vz.ErrorInvalidDiskImage, + want: "ErrorInvalidDiskImage", + }, + { + code: vz.ErrorVirtualMachineLimitExceeded, + want: "ErrorVirtualMachineLimitExceeded", + }, + { + code: vz.ErrorNetworkError, + want: "ErrorNetworkError", + }, + + { + code: vz.ErrorOutOfDiskSpace, + want: "ErrorOutOfDiskSpace", + }, + { + code: vz.ErrorOperationCancelled, + want: "ErrorOperationCancelled", + }, + { + code: vz.ErrorNotSupported, + want: "ErrorNotSupported", + }, + } + for _, tc := range cases { + got := tc.code.String() + if tc.want != got { + t.Fatalf("want %q but got %q", tc.want, got) + } + } + }) + + t.Run("macOS installation", func(t *testing.T) { + cases := []struct { + code vz.ErrorCode + want string + }{ + { + code: vz.ErrorRestoreImageCatalogLoadFailed, + want: "ErrorRestoreImageCatalogLoadFailed", + }, + { + code: vz.ErrorInvalidRestoreImageCatalog, + want: "ErrorInvalidRestoreImageCatalog", + }, + { + code: vz.ErrorNoSupportedRestoreImagesInCatalog, + want: "ErrorNoSupportedRestoreImagesInCatalog", + }, + { + code: vz.ErrorRestoreImageLoadFailed, + want: "ErrorRestoreImageLoadFailed", + }, + { + code: vz.ErrorInvalidRestoreImage, + want: "ErrorInvalidRestoreImage", + }, + { + code: vz.ErrorInstallationRequiresUpdate, + want: "ErrorInstallationRequiresUpdate", + }, + { + code: vz.ErrorInstallationFailed, + want: "ErrorInstallationFailed", + }, + } + for _, tc := range cases { + got := tc.code.String() + if tc.want != got { + t.Fatalf("want %q but got %q", tc.want, got) + } + } + }) +}