Scoop API

scoop:scoop.platform:0.1.0

src/system/errors.scoop

Selected production source · src/system/errors.scoop

1package scoop.platform.system
3/**
4 * A portable classification of operating-system failures, with native details retained separately.
5 */
6public 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 Other
81/**
82 * Identifies how a native error code must be interpreted.
83 */
84public 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 NtStatus
107/**
108 * A native error domain and code with optional underlying system code.
109 */
110public struct NativeError(
111 val domain: NativeErrorDomain,
112 val code: Int,
113 val systemCode: Int? = None
114) {
115 /**
116 * The portable classification corresponding to this native error.
117 */
118 public val kind: PlatformErrorKind get() {
119 @Unsafe
120 {
121 val domainCode =
122 when (domain) {
123 case PosixErrno -> 0
124 case AddressResolution -> 1
125 case Win32 -> 2
126 case Winsock -> 3
127 case NtStatus -> 4
128 }
129 val category = nativeErrorKind(domainCode, code, systemCode ?: 0)
130 return errorKind(category)
131 }
132 }
135/**
136 * A platform-operation failure with portable kind, optional native error, and native-byte path or
137 * operation context.
138 */
139public 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? = None
156): Exception(Some(platformErrorMessage(operation, nativeError, context)))
158private 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 message
171private fun errorKind(category: Int): PlatformErrorKind =
172 when (category) {
173 case 1 -> PlatformErrorKind.NotFound
174 case 2 -> PlatformErrorKind.AlreadyExists
175 case 3 -> PlatformErrorKind.PermissionDenied
176 case 4 -> PlatformErrorKind.NotDirectory
177 case 5 -> PlatformErrorKind.IsDirectory
178 case 6 -> PlatformErrorKind.InvalidInput
179 case 7 -> PlatformErrorKind.NoMemory
180 case 8 -> PlatformErrorKind.NoSpace
181 case 9 -> PlatformErrorKind.BrokenPipe
182 case 10 -> PlatformErrorKind.ConnectionRefused
183 case 11 -> PlatformErrorKind.ConnectionReset
184 case 12 -> PlatformErrorKind.AddressInUse
185 case 13 -> PlatformErrorKind.AddressUnavailable
186 case 14 -> PlatformErrorKind.TimedOut
187 case 15 -> PlatformErrorKind.Interrupted
188 case 16 -> PlatformErrorKind.WouldBlock
189 case 17 -> PlatformErrorKind.Unsupported
190 else -> PlatformErrorKind.Other
191 }
193@GCLeaf
194@Extern(name = "scoop_platform_error_kind")
195private fun nativeErrorKind(domain: Int, code: Int, systemCode: Int): Int