libobs_wrapper\utils/
error.rs

1use std::fmt::Display;
2
3use crate::enums::ObsResetVideoStatus;
4
5/// Error type for OBS function calls.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub enum ObsError {
8    /// The `obs_startup` function failed on libobs.
9    Failure,
10    /// This error is emitted if a dummy DLL was loaded instead of the real libobs DLL. Make sure you bootstrap properly with libobs-bootstrapper
11    InvalidDll,
12    /// Failed to lock mutex describing whether there is a
13    /// thread using libobs or not. Report to crate maintainer.
14    MutexFailure,
15    /// Some or no thread is already using libobs. This is a bug!
16    ThreadFailure,
17    /// Unable to reset video.
18    ResetVideoFailure(ObsResetVideoStatus),
19    /// Unable to reset video because the program attempted to
20    /// change the graphics module. This is a bug!
21    ResetVideoFailureGraphicsModule,
22    /// Unable to reset video because some outputs were still active.
23    ResetVideoFailureOutputActive,
24    /// The function returned a null pointer, often indicating
25    /// an error with creating the object of the requested
26    /// pointer.
27    NullPointer(Option<String>),
28    OutputAlreadyActive,
29    OutputStartFailure(Option<String>),
30    OutputStopFailure(Option<String>),
31    OutputPauseFailure(Option<String>),
32    OutputNotFound,
33    SourceNotFound,
34    SourceNotAvailable(String),
35    InvalidOperation(String),
36    /// Error converting a string between Rust and OBS
37    StringConversionError,
38
39    /// Native error from the Windows API when creating a display
40    DisplayCreationError(String),
41
42    OutputSaveBufferFailure(String),
43
44    /// The obs thread couldn't be called
45    InvocationError(String),
46
47    JsonParseError,
48    /// Couldn't get the sender of the signal
49    NoSenderError,
50    NoAvailableEncoders,
51    /// Error locking a mutex or RwLock. You should probably restart the application to avoid memory leaks.
52    LockError(String),
53    Unexpected(String),
54
55    /// Encoder is still active, stop the attached output before proceeding
56    EncoderActive,
57
58    /// Error during platform-specific initialization
59    PlatformInitError(String),
60
61    /// Error reading/writing to a file
62    IoError(String),
63
64    /// Failed to get data from signal calldata
65    SignalDataError(String),
66
67    /// Failed to convert an enum from a code
68    EnumConversionError(String),
69
70    /// Failed to send/receive on a runtime channel
71    RuntimeChannelError(String),
72
73    /// Attempted to call a OBS runtime function from outside the OBS thread.
74    /// This error should NEVER occur. If you are not using the runtime manually or have the "enable_runtime" feature enabled
75    /// then please report this to the crate maintainer as this indicates a bug in the crate.
76    RuntimeOutsideThread,
77
78    /// A filter was already applied to a source
79    FilterAlreadyApplied,
80}
81
82#[cfg_attr(coverage_nightly, coverage(off))]
83impl Display for ObsError {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(f, "OBS Error: ")?;
86
87        match self {
88            ObsError::Failure => write!(f, "`obs-startup` function failed on libobs"),
89            ObsError::MutexFailure => write!(f, "Failed to lock mutex describing whether there is a thread using libobs or not. Report to crate maintainer."),
90            ObsError::ThreadFailure => write!(f, "Some or no thread is already using libobs. This is a bug!"),
91            ObsError::ResetVideoFailure(status) => write!(f, "Could not reset obs video. Status: {:?}", status),
92            ObsError::ResetVideoFailureGraphicsModule => write!(f, "Unable to reset video because the program attempted to change the graphics module. This is a bug!"),
93            ObsError::ResetVideoFailureOutputActive => write!(f, "Unable to reset video because some outputs were still active."),
94            ObsError::NullPointer(e) => write!(f, "The function returned a null pointer, often indicating an error with creating the object of the requested pointer. Details: {:?}", e),
95            ObsError::OutputAlreadyActive => write!(f, "Output is already active."),
96            ObsError::OutputStartFailure(s) => write!(f, "Output failed to start. Error is {:?}", s),
97            ObsError::OutputStopFailure(s) => write!(f, "Output failed to stop. Error is {:?}", s),
98            ObsError::OutputNotFound => write!(f, "Output not found."),
99            ObsError::DisplayCreationError(e) => write!(f, "Native error from the Windows API when creating a display: {:?}", e),
100            ObsError::OutputSaveBufferFailure(e) => write!(f, "Couldn't save output buffer: {:?}", e),
101            ObsError::SourceNotFound => write!(f, "Source not found."),
102            ObsError::SourceNotAvailable(source_name) => write!(f, "Source {} is not available. See logs or similar to check why.", source_name),
103            ObsError::InvocationError(e) => write!(f, "The obs thread couldn't be called: {:?}", e),
104            ObsError::JsonParseError => write!(f, "Failed to parse JSON data."),
105            ObsError::NoSenderError => write!(f, "Couldn't get the sender of the signal."),
106            ObsError::NoAvailableEncoders => write!(f, "No available encoders found."),
107            ObsError::OutputPauseFailure(s) => write!(f, "Output failed to pause. Error is {:?}", s),
108            ObsError::LockError(e) => write!(f, "Error locking a mutex or RwLock: {:?}. You should probably restart the application to avoid memory leaks.", e),
109            ObsError::Unexpected(e) => write!(f, "Unexpected error: {:?}", e),
110            ObsError::EncoderActive => write!(f, "Encoder is still active, stop the attached output before proceeding"),
111            ObsError::StringConversionError => write!(f, "Error converting a string between Rust and OBS"),
112            ObsError::PlatformInitError(e) => write!(f, "Error during platform-specific initialization: {}", e),
113            ObsError::InvalidOperation(e) => write!(f, "Invalid operation: {}", e),
114            ObsError::IoError(e) => write!(f, "I/O error: {}", e),
115            ObsError::SignalDataError(e) => write!(f, "Signal data error: {}", e),
116            ObsError::EnumConversionError(e) => write!(f, "Enum conversion error: {}", e),
117            ObsError::RuntimeChannelError(e) => write!(f, "Runtime channel error: {}", e),
118            ObsError::InvalidDll => write!(f, "A dummy DLL was loaded instead of the real libobs DLL. Make sure you bootstrap properly with libobs-bootstrapper"),
119            #[cfg(feature="enable_runtime")]
120            ObsError::RuntimeOutsideThread => write!(f, "Attempted to call a OBS runtime function from outside the OBS thread. This is a bug in the crate!"),
121            #[cfg(not(feature="enable_runtime"))]
122            ObsError::RuntimeOutsideThread => write!(f, "Attempted to call a OBS runtime function from outside the OBS thread. Make sure that you do not use any OBS struct from a different thread than the one where the ObsContext was initialized. THIS BUG WILL CAUSE MEMORY CORRUPTION OR DEADLOCKS!"),
123            ObsError::FilterAlreadyApplied => write!(f, "Filter was applied already."),
124        }
125    }
126}
127
128impl std::error::Error for ObsError {}