mssf_core/runtime/
error.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, WString};
7
8// Fills the error info as string for better debugging.
9// SF has separate last error set and get from windows.
10// Not all error strings are set by SF. This is not very useful in practice.
11pub fn fill_fabric_hresult(code: HRESULT) -> crate::WinError {
12    // in rs, this function always succeed. The fail case is that the return ptr is null.
13    let sf_err = crate::API_TABLE.fabric_get_last_error_message().unwrap();
14    let err_str_raw = unsafe { sf_err.get_String() };
15    let err_str = if err_str_raw.is_null() {
16        &[]
17    } else {
18        unsafe { err_str_raw.as_wide() }
19    };
20    println!("debug std: {}", WString::from_wide(err_str));
21    crate::WinError::new(code, WString::from_wide(err_str).to_string())
22}
23
24pub fn fill_fabric_error(e: crate::WinError) -> crate::WinError {
25    fill_fabric_hresult(e.code())
26}
27
28#[cfg(test)]
29#[cfg(windows)] // linux error propagate is not working yet
30mod test {
31    use crate::{WString, WinError};
32    use mssf_com::FabricTypes::FABRIC_E_GATEWAY_NOT_REACHABLE;
33
34    #[test]
35    fn test_win_error() {
36        let s = WString::from("MyError");
37        let e = WinError::new(
38            crate::HRESULT(FABRIC_E_GATEWAY_NOT_REACHABLE.0),
39            s.clone().to_string(),
40        );
41        assert_eq!(e.message(), s.to_string_lossy());
42    }
43}