rafx_api/
error.rs

1#[cfg(feature = "rafx-vulkan")]
2use ash::vk;
3use std::sync::Arc;
4
5pub type RafxResult<T> = Result<T, RafxError>;
6
7/// Generic error that contains all the different kinds of errors that may occur when using the API
8#[derive(Debug, Clone)]
9pub enum RafxError {
10    StringError(String),
11    ValidationRequiredButUnavailable,
12    IoError(Arc<std::io::Error>),
13    #[cfg(feature = "rafx-dx12")]
14    WindowsApiError(windows::core::Error),
15    #[cfg(feature = "rafx-dx12")]
16    HResult(windows::core::HRESULT),
17    #[cfg(feature = "rafx-dx12")]
18    HassleError(Arc<hassle_rs::HassleError>),
19    #[cfg(feature = "rafx-vulkan")]
20    VkError(vk::Result),
21    #[cfg(feature = "rafx-vulkan")]
22    VkLoadingError(Arc<ash::LoadingError>),
23    #[cfg(any(feature = "rafx-dx12", feature = "rafx-vulkan",))]
24    AllocationError(Arc<gpu_allocator::AllocationError>),
25    #[cfg(any(feature = "rafx-gles2", feature = "rafx-gles3"))]
26    GlError(u32),
27}
28
29impl std::error::Error for RafxError {
30    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
31        match *self {
32            RafxError::StringError(_) => None,
33            RafxError::ValidationRequiredButUnavailable => None,
34            RafxError::IoError(ref e) => Some(&**e),
35
36            #[cfg(feature = "rafx-dx12")]
37            RafxError::WindowsApiError(ref e) => Some(e),
38            #[cfg(feature = "rafx-dx12")]
39            RafxError::HResult(ref e) => None,
40            #[cfg(feature = "rafx-dx12")]
41            RafxError::HassleError(ref e) => Some(e),
42            #[cfg(feature = "rafx-vulkan")]
43            RafxError::VkError(ref e) => Some(e),
44            #[cfg(feature = "rafx-vulkan")]
45            RafxError::VkLoadingError(ref e) => Some(&**e),
46            #[cfg(any(feature = "rafx-dx12", feature = "rafx-vulkan",))]
47            RafxError::AllocationError(ref e) => Some(&**e),
48            #[cfg(any(feature = "rafx-gles2", feature = "rafx-gles3"))]
49            RafxError::GlError(_) => None,
50        }
51    }
52}
53
54impl core::fmt::Display for RafxError {
55    fn fmt(
56        &self,
57        fmt: &mut core::fmt::Formatter,
58    ) -> core::fmt::Result {
59        match *self {
60            RafxError::StringError(ref e) => e.fmt(fmt),
61            RafxError::ValidationRequiredButUnavailable => {
62                "ValidationRequiredButUnavailable".fmt(fmt)
63            }
64            RafxError::IoError(ref e) => e.fmt(fmt),
65            #[cfg(feature = "rafx-dx12")]
66            RafxError::WindowsApiError(ref e) => e.fmt(fmt),
67            #[cfg(feature = "rafx-dx12")]
68            RafxError::HResult(ref e) => e.fmt(fmt),
69            #[cfg(feature = "rafx-dx12")]
70            RafxError::HassleError(ref e) => e.fmt(fmt),
71            #[cfg(feature = "rafx-vulkan")]
72            RafxError::VkError(ref e) => e.fmt(fmt),
73            #[cfg(feature = "rafx-vulkan")]
74            RafxError::VkLoadingError(ref e) => e.fmt(fmt),
75            #[cfg(any(feature = "rafx-dx12", feature = "rafx-vulkan",))]
76            RafxError::AllocationError(ref e) => e.fmt(fmt),
77            #[cfg(any(feature = "rafx-gles2", feature = "rafx-gles3"))]
78            RafxError::GlError(ref e) => e.fmt(fmt),
79        }
80    }
81}
82
83impl From<&str> for RafxError {
84    fn from(str: &str) -> Self {
85        RafxError::StringError(str.to_string())
86    }
87}
88
89impl From<String> for RafxError {
90    fn from(string: String) -> Self {
91        RafxError::StringError(string)
92    }
93}
94
95impl From<std::io::Error> for RafxError {
96    fn from(error: std::io::Error) -> Self {
97        RafxError::IoError(Arc::new(error))
98    }
99}
100
101#[cfg(feature = "rafx-dx12")]
102impl From<windows::core::Error> for RafxError {
103    fn from(result: windows::core::Error) -> Self {
104        RafxError::WindowsApiError(result)
105    }
106}
107
108#[cfg(feature = "rafx-dx12")]
109impl From<windows::core::HRESULT> for RafxError {
110    fn from(result: windows::core::HRESULT) -> Self {
111        RafxError::HResult(result)
112    }
113}
114
115#[cfg(feature = "rafx-dx12")]
116impl From<hassle_rs::HassleError> for RafxError {
117    fn from(result: hassle_rs::HassleError) -> Self {
118        RafxError::HassleError(Arc::new(result))
119    }
120}
121
122#[cfg(feature = "rafx-vulkan")]
123impl From<vk::Result> for RafxError {
124    fn from(result: vk::Result) -> Self {
125        RafxError::VkError(result)
126    }
127}
128
129#[cfg(feature = "rafx-vulkan")]
130impl From<ash::LoadingError> for RafxError {
131    fn from(result: ash::LoadingError) -> Self {
132        RafxError::VkLoadingError(Arc::new(result))
133    }
134}
135
136#[cfg(any(feature = "rafx-dx12", feature = "rafx-vulkan",))]
137impl From<gpu_allocator::AllocationError> for RafxError {
138    fn from(error: gpu_allocator::AllocationError) -> Self {
139        RafxError::AllocationError(Arc::new(error))
140    }
141}