scoop:scoop.platform:0.1.0
src/system/errors.scoop
Selected production source · src/system/errors.scoop
1
package scoop.platform.system3
/**4
* A portable classification of operating-system failures, with native details retained separately.5
*/6
public enum PlatformErrorKind {7
/**8
* The requested resource does not exist.9
*/10
NotFound,11
/**12
* The operation requires an absent destination.13
*/14
AlreadyExists,15
/**16
* Access was denied.17
*/18
PermissionDenied,19
/**20
* A required path component is not a directory.21
*/22
NotDirectory,23
/**24
* The selected entry is a directory where a file is required.25
*/26
IsDirectory,27
/**28
* The native operation rejected its arguments.29
*/30
InvalidInput,31
/**32
* The native operation could not allocate required memory.33
*/34
NoMemory,35
/**36
* The storage destination has insufficient space.37
*/38
NoSpace,39
/**40
* The output pipe has no reader.41
*/42
BrokenPipe,43
/**44
* The remote endpoint refused the connection.45
*/46
ConnectionRefused,47
/**48
* The connection was reset.49
*/50
ConnectionReset,51
/**52
* The requested local address is already in use.53
*/54
AddressInUse,55
/**56
* The requested address is unavailable on this host.57
*/58
AddressUnavailable,59
/**60
* The native operation timed out.61
*/62
TimedOut,63
/**64
* A native wait was interrupted.65
*/66
Interrupted,67
/**68
* The operation cannot progress without waiting.69
*/70
WouldBlock,71
/**72
* The requested operation is unsupported.73
*/74
Unsupported,75
/**76
* A native failure outside the portable categories.77
*/78
Other79
}81
/**82
* Identifies how a native error code must be interpreted.83
*/84
public enum NativeErrorDomain {85
/**86
* A POSIX errno value.87
*/88
PosixErrno,89
/**90
* A native address-resolution error code.91
*/92
AddressResolution,93
/**94
* A Win32 error code captured from the failing operation.95
*/96
Win32,97
/**98
* A Windows Sockets error code captured from the failing operation.99
*/100
Winsock,101
/**102
* An NTSTATUS returned directly by a Windows native operation.103
*/104
NtStatus105
}107
/**108
* A native error domain and code with optional underlying system code.109
*/110
public struct NativeError(111
val domain: NativeErrorDomain,112
val code: Int,113
val systemCode: Int? = None114
) {115
/**116
* The portable classification corresponding to this native error.117
*/118
public val kind: PlatformErrorKind get() {119
@Unsafe120
{121
val domainCode =122
when (domain) {123
case PosixErrno -> 0124
case AddressResolution -> 1125
case Win32 -> 2126
case Winsock -> 3127
case NtStatus -> 4128
}129
val category = nativeErrorKind(domainCode, code, systemCode ?: 0)130
return errorKind(category)131
}132
}133
}135
/**136
* A platform-operation failure with portable kind, optional native error, and native-byte path or137
* operation context.138
*/139
public class PlatformException public constructor(140
/**141
* The operation that failed.142
*/143
public val operation: String,144
/**145
* The portable failure category.146
*/147
public val kind: PlatformErrorKind,148
/**149
* The original native error, when available.150
*/151
public val nativeError: NativeError? = None,152
/**153
* An optional native-byte path or other operation-specific context.154
*/155
public val context: OsString? = None156
): Exception(Some(platformErrorMessage(operation, nativeError, context)))158
private fun platformErrorMessage(159
operation: String,160
error: NativeError?,161
context: OsString?162
): String {163
var message = operation + " failed"164
if (error != None)165
message += " (native code " + error!!.code.toString() + ")"166
if (context != None)167
message += ": " + context!!.display()168
return message169
}171
private fun errorKind(category: Int): PlatformErrorKind =172
when (category) {173
case 1 -> PlatformErrorKind.NotFound174
case 2 -> PlatformErrorKind.AlreadyExists175
case 3 -> PlatformErrorKind.PermissionDenied176
case 4 -> PlatformErrorKind.NotDirectory177
case 5 -> PlatformErrorKind.IsDirectory178
case 6 -> PlatformErrorKind.InvalidInput179
case 7 -> PlatformErrorKind.NoMemory180
case 8 -> PlatformErrorKind.NoSpace181
case 9 -> PlatformErrorKind.BrokenPipe182
case 10 -> PlatformErrorKind.ConnectionRefused183
case 11 -> PlatformErrorKind.ConnectionReset184
case 12 -> PlatformErrorKind.AddressInUse185
case 13 -> PlatformErrorKind.AddressUnavailable186
case 14 -> PlatformErrorKind.TimedOut187
case 15 -> PlatformErrorKind.Interrupted188
case 16 -> PlatformErrorKind.WouldBlock189
case 17 -> PlatformErrorKind.Unsupported190
else -> PlatformErrorKind.Other191
}193
@GCLeaf194
@Extern(name = "scoop_platform_error_kind")195
private fun nativeErrorKind(domain: Int, code: Int, systemCode: Int): Int