mssf_core/error/
errorcode.rs

1// ------------------------------------------------------------
2// Copyright (c) Microsoft Corporation.  All rights reserved.
3// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
4// ------------------------------------------------------------
5
6use crate::HRESULT;
7use mssf_com::FabricTypes::FABRIC_ERROR_CODE;
8
9// Common HRESULT codes that SF reuses from windows.
10const S_OK: FABRIC_ERROR_CODE = FABRIC_ERROR_CODE(windows_core::Win32::Foundation::S_OK.0);
11const E_ABORT: FABRIC_ERROR_CODE = FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_ABORT.0);
12const E_ACCESSDENIED: FABRIC_ERROR_CODE =
13    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_ACCESSDENIED.0);
14const E_FAIL: FABRIC_ERROR_CODE = FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_FAIL.0);
15const E_HANDLE: FABRIC_ERROR_CODE = FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_HANDLE.0);
16const E_INVALIDARG: FABRIC_ERROR_CODE =
17    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_INVALIDARG.0);
18const E_NOINTERFACE: FABRIC_ERROR_CODE =
19    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_NOINTERFACE.0);
20const E_NOTIMPL: FABRIC_ERROR_CODE =
21    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_NOTIMPL.0);
22const E_OUTOFMEMORY: FABRIC_ERROR_CODE =
23    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_OUTOFMEMORY.0);
24const E_POINTER: FABRIC_ERROR_CODE =
25    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_POINTER.0);
26const E_UNEXPECTED: FABRIC_ERROR_CODE =
27    FABRIC_ERROR_CODE(windows_core::Win32::Foundation::E_UNEXPECTED.0);
28
29// HRESULT codes from win32 errors that SF resuses.
30const E_FILE_EXISTS: FABRIC_ERROR_CODE =
31    FABRIC_ERROR_CODE(HRESULT::from_win32(windows_core::Win32::Foundation::ERROR_FILE_EXISTS.0).0);
32const E_DIR_NOT_EMPTY: FABRIC_ERROR_CODE = FABRIC_ERROR_CODE(
33    HRESULT::from_win32(windows_core::Win32::Foundation::ERROR_DIR_NOT_EMPTY.0).0,
34);
35const E_NOT_FOUND: FABRIC_ERROR_CODE =
36    FABRIC_ERROR_CODE(HRESULT::from_win32(windows_core::Win32::Foundation::ERROR_NOT_FOUND.0).0);
37
38/// Hepler macro to define fabric error code.
39/// SF uses win32 hresult code together with the custom fabric error code.
40/// This enum helps passing errors between Rust and SF API, and is easy to debug.
41/// code1 list are windows errors, lit is a dummy string literal, code list are fabric errors.
42/// The macro defines windows errors first, then followed by fabric errors.
43// Need to match cs impl:
44// https://github.com/microsoft/service-fabric/blob/19791eb97c8d876517daa030e5a403f4bcad25b1/src/prod/src/managed/Api/src/System/Fabric/Interop/NativeTypes.cs#L57
45macro_rules! define_fabric_error_code{
46    ($( $code1:ident ),* ,($lit:literal), $( $code:ident ),*) =>{
47        #[allow(non_camel_case_types)]
48        #[derive(Debug, Clone, PartialEq)]
49        #[repr(i32)]
50        pub enum ErrorCode {
51            // Define windows error codes for SF
52            $(
53                $code1 = $code1 .0,
54            )*
55
56            // defines SF error codes.
57            $(
58                $code = mssf_com::FabricTypes::$code .0,
59            )*
60        }
61
62        impl TryFrom<FABRIC_ERROR_CODE> for ErrorCode {
63            type Error = &'static str;
64
65            fn try_from(value: FABRIC_ERROR_CODE) -> Result<Self, Self::Error> {
66                match value {
67                    $(
68                        $code1 => Ok(Self::$code1),
69                    )*
70                    // SF code converts.
71                    $(
72                        mssf_com::FabricTypes::$code => Ok(Self::$code),
73                    )*
74                    _ => Err("Unknown FABRIC_ERROR_CODE")
75                }
76            }
77        }
78    }
79}
80
81impl From<ErrorCode> for crate::Error {
82    fn from(value: ErrorCode) -> Self {
83        crate::Error(HRESULT(value as i32))
84    }
85}
86
87// other conversions goes through crate::Error
88impl From<ErrorCode> for HRESULT {
89    fn from(value: ErrorCode) -> Self {
90        crate::Error::from(value).into()
91    }
92}
93
94impl From<ErrorCode> for crate::WinError {
95    fn from(value: ErrorCode) -> Self {
96        crate::Error::from(value).into()
97    }
98}
99
100impl core::fmt::Display for ErrorCode {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        core::write!(f, "{self:?}") // use the debug string
103    }
104}
105
106impl ErrorCode {
107    /// Matches the FabricTransientException mapping in InteropExceptionMap.cs
108    pub fn is_transient(&self) -> bool {
109        matches!(
110            self,
111            crate::ErrorCode::FABRIC_E_SERVICE_OFFLINE
112                | crate::ErrorCode::FABRIC_E_APPLICATION_UPDATE_IN_PROGRESS
113                | crate::ErrorCode::FABRIC_E_RECONFIGURATION_PENDING
114                | crate::ErrorCode::FABRIC_E_NO_WRITE_QUORUM
115                | crate::ErrorCode::FABRIC_E_REPLICATION_QUEUE_FULL
116                | crate::ErrorCode::FABRIC_E_SERVICE_TOO_BUSY
117                | crate::ErrorCode::FABRIC_E_GATEWAY_NOT_REACHABLE
118                | crate::ErrorCode::E_ABORT // This is the same as OperationCanceled.
119                | crate::ErrorCode::FABRIC_E_ACQUIRE_FILE_LOCK_FAILED
120                | crate::ErrorCode::FABRIC_E_IMAGEBUILDER_TIMEOUT
121                | crate::ErrorCode::FABRIC_E_CONSTRAINT_KEY_UNDEFINED
122                | crate::ErrorCode::FABRIC_E_STOP_IN_PROGRESS
123                | crate::ErrorCode::FABRIC_E_DATABASE_MIGRATION_IN_PROGRESS
124        )
125    }
126}
127
128// This defines all the fabric error codes.
129// list copied from https://github.com/microsoft/service-fabric/blob/19791eb97c8d876517daa030e5a403f4bcad25b1/src/prod/src/idl/public/FabricTypes.idl#L60C18-L60C35
130define_fabric_error_code!(
131    S_OK,
132    E_ABORT,
133    E_ACCESSDENIED,
134    E_FAIL,
135    E_HANDLE,
136    E_INVALIDARG,
137    E_NOINTERFACE,
138    E_NOTIMPL,
139    E_OUTOFMEMORY,
140    E_POINTER,
141    E_UNEXPECTED,
142    E_FILE_EXISTS,
143    E_DIR_NOT_EMPTY,
144    E_NOT_FOUND,
145    ("Literal for breaking up first error chunk and the fabric errors"),
146    FABRIC_E_COMMUNICATION_ERROR,
147    FABRIC_E_INVALID_ADDRESS,
148    FABRIC_E_INVALID_NAME_URI,
149    FABRIC_E_INVALID_PARTITION_KEY,
150    FABRIC_E_NAME_ALREADY_EXISTS,
151    FABRIC_E_NAME_DOES_NOT_EXIST,
152    FABRIC_E_NAME_NOT_EMPTY,
153    FABRIC_E_NODE_NOT_FOUND,
154    FABRIC_E_NODE_IS_UP,
155    FABRIC_E_NO_WRITE_QUORUM,
156    FABRIC_E_NOT_PRIMARY,
157    FABRIC_E_NOT_READY,
158    FABRIC_E_OPERATION_NOT_COMPLETE,
159    FABRIC_E_PROPERTY_DOES_NOT_EXIST,
160    FABRIC_E_RECONFIGURATION_PENDING,
161    FABRIC_E_REPLICATION_QUEUE_FULL,
162    FABRIC_E_SERVICE_ALREADY_EXISTS,
163    FABRIC_E_SERVICE_DOES_NOT_EXIST,
164    FABRIC_E_SERVICE_OFFLINE,
165    FABRIC_E_SERVICE_METADATA_MISMATCH,
166    FABRIC_E_SERVICE_AFFINITY_CHAIN_NOT_SUPPORTED,
167    FABRIC_E_SERVICE_TYPE_ALREADY_REGISTERED,
168    FABRIC_E_SERVICE_TYPE_NOT_REGISTERED,
169    FABRIC_E_VALUE_TOO_LARGE,
170    FABRIC_E_VALUE_EMPTY,
171    FABRIC_E_PROPERTY_CHECK_FAILED,
172    FABRIC_E_WRITE_CONFLICT,
173    FABRIC_E_ENUMERATION_COMPLETED,
174    FABRIC_E_APPLICATION_TYPE_PROVISION_IN_PROGRESS,
175    FABRIC_E_APPLICATION_TYPE_ALREADY_EXISTS,
176    FABRIC_E_APPLICATION_TYPE_NOT_FOUND,
177    FABRIC_E_APPLICATION_TYPE_IN_USE,
178    FABRIC_E_APPLICATION_ALREADY_EXISTS,
179    FABRIC_E_APPLICATION_NOT_FOUND,
180    FABRIC_E_APPLICATION_UPGRADE_IN_PROGRESS,
181    FABRIC_E_APPLICATION_UPGRADE_VALIDATION_ERROR,
182    FABRIC_E_SERVICE_TYPE_NOT_FOUND,
183    FABRIC_E_SERVICE_TYPE_MISMATCH,
184    FABRIC_E_SERVICE_TYPE_TEMPLATE_NOT_FOUND,
185    FABRIC_E_CONFIGURATION_SECTION_NOT_FOUND,
186    FABRIC_E_CONFIGURATION_PARAMETER_NOT_FOUND,
187    FABRIC_E_INVALID_CONFIGURATION,
188    FABRIC_E_IMAGEBUILDER_VALIDATION_ERROR,
189    FABRIC_E_PARTITION_NOT_FOUND,
190    FABRIC_E_REPLICA_DOES_NOT_EXIST,
191    FABRIC_E_SERVICE_GROUP_ALREADY_EXISTS,
192    FABRIC_E_SERVICE_GROUP_DOES_NOT_EXIST,
193    FABRIC_E_PROCESS_DEACTIVATED,
194    FABRIC_E_PROCESS_ABORTED,
195    FABRIC_E_UPGRADE_FAILED,
196    FABRIC_E_INVALID_CREDENTIAL_TYPE,
197    FABRIC_E_INVALID_X509_FIND_TYPE,
198    FABRIC_E_INVALID_X509_STORE_LOCATION,
199    FABRIC_E_INVALID_X509_STORE_NAME,
200    FABRIC_E_INVALID_X509_THUMBPRINT,
201    FABRIC_E_INVALID_PROTECTION_LEVEL,
202    FABRIC_E_INVALID_X509_STORE,
203    FABRIC_E_INVALID_SUBJECT_NAME,
204    FABRIC_E_INVALID_ALLOWED_COMMON_NAME_LIST,
205    FABRIC_E_INVALID_CREDENTIALS,
206    FABRIC_E_DECRYPTION_FAILED,
207    FABRIC_E_CONFIGURATION_PACKAGE_NOT_FOUND,
208    FABRIC_E_DATA_PACKAGE_NOT_FOUND,
209    FABRIC_E_CODE_PACKAGE_NOT_FOUND,
210    FABRIC_E_SERVICE_ENDPOINT_RESOURCE_NOT_FOUND,
211    FABRIC_E_INVALID_OPERATION,
212    FABRIC_E_OBJECT_CLOSED,
213    FABRIC_E_TIMEOUT,
214    FABRIC_E_FILE_NOT_FOUND,
215    FABRIC_E_DIRECTORY_NOT_FOUND,
216    FABRIC_E_INVALID_DIRECTORY,
217    FABRIC_E_PATH_TOO_LONG,
218    FABRIC_E_IMAGESTORE_IOERROR,
219    FABRIC_E_CORRUPTED_IMAGE_STORE_OBJECT_FOUND,
220    FABRIC_E_APPLICATION_NOT_UPGRADING,
221    FABRIC_E_APPLICATION_ALREADY_IN_TARGET_VERSION,
222    FABRIC_E_IMAGEBUILDER_UNEXPECTED_ERROR,
223    FABRIC_E_FABRIC_VERSION_NOT_FOUND,
224    FABRIC_E_FABRIC_VERSION_IN_USE,
225    FABRIC_E_FABRIC_VERSION_ALREADY_EXISTS,
226    FABRIC_E_FABRIC_ALREADY_IN_TARGET_VERSION,
227    FABRIC_E_FABRIC_NOT_UPGRADING,
228    FABRIC_E_FABRIC_UPGRADE_IN_PROGRESS,
229    FABRIC_E_FABRIC_UPGRADE_VALIDATION_ERROR,
230    FABRIC_E_HEALTH_MAX_REPORTS_REACHED,
231    FABRIC_E_HEALTH_STALE_REPORT,
232    FABRIC_E_KEY_TOO_LARGE,
233    FABRIC_E_KEY_NOT_FOUND,
234    FABRIC_E_SEQUENCE_NUMBER_CHECK_FAILED,
235    FABRIC_E_ENCRYPTION_FAILED,
236    FABRIC_E_INVALID_ATOMIC_GROUP,
237    FABRIC_E_HEALTH_ENTITY_NOT_FOUND,
238    FABRIC_E_SERVICE_MANIFEST_NOT_FOUND,
239    FABRIC_E_RELIABLE_SESSION_TRANSPORT_STARTUP_FAILURE,
240    FABRIC_E_RELIABLE_SESSION_ALREADY_EXISTS,
241    FABRIC_E_RELIABLE_SESSION_CANNOT_CONNECT,
242    FABRIC_E_RELIABLE_SESSION_MANAGER_EXISTS,
243    FABRIC_E_RELIABLE_SESSION_REJECTED,
244    FABRIC_E_RELIABLE_SESSION_MANAGER_ALREADY_LISTENING,
245    FABRIC_E_RELIABLE_SESSION_MANAGER_NOT_FOUND,
246    FABRIC_E_RELIABLE_SESSION_MANAGER_NOT_LISTENING,
247    FABRIC_E_INVALID_SERVICE_TYPE,
248    FABRIC_E_IMAGEBUILDER_TIMEOUT,
249    FABRIC_E_IMAGEBUILDER_ACCESS_DENIED,
250    FABRIC_E_IMAGEBUILDER_INVALID_MSI_FILE,
251    FABRIC_E_SERVICE_TOO_BUSY,
252    FABRIC_E_TRANSACTION_NOT_ACTIVE,
253    FABRIC_E_REPAIR_TASK_ALREADY_EXISTS,
254    FABRIC_E_REPAIR_TASK_NOT_FOUND,
255    FABRIC_E_RELIABLE_SESSION_NOT_FOUND,
256    FABRIC_E_RELIABLE_SESSION_QUEUE_EMPTY,
257    FABRIC_E_RELIABLE_SESSION_QUOTA_EXCEEDED,
258    FABRIC_E_RELIABLE_SESSION_SERVICE_FAULTED,
259    FABRIC_E_RELIABLE_SESSION_INVALID_TARGET_PARTITION,
260    FABRIC_E_TRANSACTION_TOO_LARGE,
261    FABRIC_E_REPLICATION_OPERATION_TOO_LARGE,
262    FABRIC_E_INSTANCE_ID_MISMATCH,
263    FABRIC_E_UPGRADE_DOMAIN_ALREADY_COMPLETED,
264    FABRIC_E_NODE_HAS_NOT_STOPPED_YET,
265    FABRIC_E_INSUFFICIENT_CLUSTER_CAPACITY,
266    FABRIC_E_INVALID_PACKAGE_SHARING_POLICY,
267    FABRIC_E_PREDEPLOYMENT_NOT_ALLOWED,
268    FABRIC_E_INVALID_BACKUP_SETTING,
269    FABRIC_E_MISSING_FULL_BACKUP,
270    FABRIC_E_BACKUP_IN_PROGRESS,
271    FABRIC_E_DUPLICATE_SERVICE_NOTIFICATION_FILTER_NAME,
272    FABRIC_E_INVALID_REPLICA_OPERATION,
273    FABRIC_E_INVALID_REPLICA_STATE,
274    FABRIC_E_LOADBALANCER_NOT_READY,
275    FABRIC_E_INVALID_PARTITION_OPERATION,
276    FABRIC_E_PRIMARY_ALREADY_EXISTS,
277    FABRIC_E_SECONDARY_ALREADY_EXISTS,
278    FABRIC_E_BACKUP_DIRECTORY_NOT_EMPTY,
279    FABRIC_E_FORCE_NOT_SUPPORTED_FOR_REPLICA_OPERATION,
280    FABRIC_E_ACQUIRE_FILE_LOCK_FAILED,
281    FABRIC_E_CONNECTION_DENIED,
282    FABRIC_E_SERVER_AUTHENTICATION_FAILED,
283    FABRIC_E_CONSTRAINT_KEY_UNDEFINED,
284    FABRIC_E_MULTITHREADED_TRANSACTIONS_NOT_ALLOWED,
285    FABRIC_E_INVALID_X509_NAME_LIST,
286    FABRIC_E_VERBOSE_FM_PLACEMENT_HEALTH_REPORTING_REQUIRED,
287    FABRIC_E_GATEWAY_NOT_REACHABLE,
288    FABRIC_E_USER_ROLE_CLIENT_CERTIFICATE_NOT_CONFIGURED,
289    FABRIC_E_TRANSACTION_ABORTED,
290    FABRIC_E_CANNOT_CONNECT,
291    FABRIC_E_MESSAGE_TOO_LARGE,
292    FABRIC_E_CONSTRAINT_NOT_SATISFIED,
293    FABRIC_E_ENDPOINT_NOT_FOUND,
294    FABRIC_E_APPLICATION_UPDATE_IN_PROGRESS,
295    FABRIC_E_DELETE_BACKUP_FILE_FAILED,
296    FABRIC_E_CONNECTION_CLOSED_BY_REMOTE_END,
297    FABRIC_E_INVALID_TEST_COMMAND_STATE,
298    FABRIC_E_TEST_COMMAND_OPERATION_ID_ALREADY_EXISTS,
299    FABRIC_E_CM_OPERATION_FAILED,
300    FABRIC_E_IMAGEBUILDER_RESERVED_DIRECTORY_ERROR,
301    FABRIC_E_CERTIFICATE_NOT_FOUND,
302    FABRIC_E_CHAOS_ALREADY_RUNNING,
303    FABRIC_E_FABRIC_DATA_ROOT_NOT_FOUND,
304    FABRIC_E_INVALID_RESTORE_DATA,
305    FABRIC_E_DUPLICATE_BACKUPS,
306    FABRIC_E_INVALID_BACKUP_CHAIN,
307    FABRIC_E_STOP_IN_PROGRESS,
308    FABRIC_E_ALREADY_STOPPED,
309    FABRIC_E_NODE_IS_DOWN,
310    FABRIC_E_NODE_TRANSITION_IN_PROGRESS,
311    FABRIC_E_INVALID_BACKUP,
312    FABRIC_E_INVALID_INSTANCE_ID,
313    FABRIC_E_INVALID_DURATION,
314    FABRIC_E_RESTORE_SAFE_CHECK_FAILED,
315    FABRIC_E_CONFIG_UPGRADE_FAILED,
316    FABRIC_E_UPLOAD_SESSION_RANGE_NOT_SATISFIABLE,
317    FABRIC_E_UPLOAD_SESSION_ID_CONFLICT,
318    FABRIC_E_INVALID_PARTITION_SELECTOR,
319    FABRIC_E_INVALID_REPLICA_SELECTOR,
320    FABRIC_E_DNS_SERVICE_NOT_FOUND,
321    FABRIC_E_INVALID_DNS_NAME,
322    FABRIC_E_DNS_NAME_IN_USE,
323    FABRIC_E_COMPOSE_DEPLOYMENT_ALREADY_EXISTS,
324    FABRIC_E_COMPOSE_DEPLOYMENT_NOT_FOUND,
325    FABRIC_E_INVALID_FOR_STATEFUL_SERVICES,
326    FABRIC_E_INVALID_FOR_STATELESS_SERVICES,
327    FABRIC_E_ONLY_VALID_FOR_STATEFUL_PERSISTENT_SERVICES,
328    FABRIC_E_INVALID_UPLOAD_SESSION_ID,
329    FABRIC_E_BACKUP_NOT_ENABLED,
330    FABRIC_E_BACKUP_IS_ENABLED,
331    FABRIC_E_BACKUP_POLICY_DOES_NOT_EXIST,
332    FABRIC_E_BACKUP_POLICY_ALREADY_EXISTS,
333    FABRIC_E_RESTORE_IN_PROGRESS,
334    FABRIC_E_RESTORE_SOURCE_TARGET_PARTITION_MISMATCH,
335    FABRIC_E_FAULT_ANALYSIS_SERVICE_NOT_ENABLED,
336    FABRIC_E_CONTAINER_NOT_FOUND,
337    FABRIC_E_OBJECT_DISPOSED,
338    FABRIC_E_NOT_READABLE,
339    FABRIC_E_BACKUPCOPIER_UNEXPECTED_ERROR,
340    FABRIC_E_BACKUPCOPIER_TIMEOUT,
341    FABRIC_E_BACKUPCOPIER_ACCESS_DENIED,
342    FABRIC_E_INVALID_SERVICE_SCALING_POLICY,
343    FABRIC_E_SINGLE_INSTANCE_APPLICATION_ALREADY_EXISTS,
344    FABRIC_E_SINGLE_INSTANCE_APPLICATION_NOT_FOUND,
345    FABRIC_E_VOLUME_ALREADY_EXISTS,
346    FABRIC_E_VOLUME_NOT_FOUND,
347    FABRIC_E_DATABASE_MIGRATION_IN_PROGRESS,
348    FABRIC_E_CENTRAL_SECRET_SERVICE_GENERIC,
349    FABRIC_E_SECRET_INVALID,
350    FABRIC_E_SECRET_VERSION_ALREADY_EXISTS,
351    FABRIC_E_SINGLE_INSTANCE_APPLICATION_UPGRADE_IN_PROGRESS,
352    FABRIC_E_OPERATION_NOT_SUPPORTED,
353    FABRIC_E_COMPOSE_DEPLOYMENT_NOT_UPGRADING,
354    FABRIC_E_SECRET_TYPE_CANNOT_BE_CHANGED,
355    FABRIC_E_NETWORK_NOT_FOUND,
356    FABRIC_E_NETWORK_IN_USE,
357    FABRIC_E_ENDPOINT_NOT_REFERENCED
358);