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    /// Failed to lock mutex describing whether there is a
11    /// thread using libobs or not. Report to crate maintainer.
12    MutexFailure,
13    /// Some or no thread is already using libobs. This is a bug!
14    ThreadFailure,
15    /// Unable to reset video.
16    ResetVideoFailure(ObsResetVideoStatus),
17    /// Unable to reset video because the program attempted to
18    /// change the graphics module. This is a bug!
19    ResetVideoFailureGraphicsModule,
20    /// Unable to reset video because some outputs were still active.
21    ResetVideoFailureOutputActive,
22    /// The function returned a null pointer, often indicating
23    /// an error with creating the object of the requested
24    /// pointer.
25    NullPointer,
26    OutputAlreadyActive,
27    OutputStartFailure(Option<String>),
28    OutputStopFailure(Option<String>),
29    OutputPauseFailure(Option<String>),
30    OutputNotFound,
31    SourceNotFound,
32    SourceNotAvailable(String),
33    InvalidOperation(String),
34    /// Error converting a string between Rust and OBS
35    StringConversionError,
36
37    /// Native error from the Windows API when creating a display
38    DisplayCreationError(String),
39
40    OutputSaveBufferFailure(String),
41
42    /// The obs thread couldn't be called
43    InvocationError(String),
44
45    JsonParseError,
46    /// Couldn't get the sender of the signal
47    NoSenderError,
48    NoAvailableEncoders,
49    /// Error locking a mutex or RwLock
50    LockError(String),
51    Unexpected(String),
52
53    /// Encoder is still active, stop the attached output before proceeding
54    EncoderActive,
55
56    /// Error during platform-specific initialization
57    PlatformInitError(String),
58}
59
60#[cfg_attr(coverage_nightly, coverage(off))]
61impl Display for ObsError {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "OBS Error: ")?;
64
65        match self {
66            ObsError::Failure => write!(f, "`obs-startup` function failed on libobs"),
67            ObsError::MutexFailure => write!(f, "Failed to lock mutex describing whether there is a thread using libobs or not. Report to crate maintainer."),
68            ObsError::ThreadFailure => write!(f, "Some or no thread is already using libobs. This is a bug!"),
69            ObsError::ResetVideoFailure(status) => write!(f, "Could not reset obs video. Status: {:?}", status),
70            ObsError::ResetVideoFailureGraphicsModule => write!(f, "Unable to reset video because the program attempted to change the graphics module. This is a bug!"),
71            ObsError::ResetVideoFailureOutputActive => write!(f, "Unable to reset video because some outputs were still active."),
72            ObsError::NullPointer => write!(f, "The function returned a null pointer, often indicating an error with creating the object of the requested pointer."),
73            ObsError::OutputAlreadyActive => write!(f, "Output is already active."),
74            ObsError::OutputStartFailure(s) => write!(f, "Output failed to start. Error is {:?}", s),
75            ObsError::OutputStopFailure(s) => write!(f, "Output failed to stop. Error is {:?}", s),
76            ObsError::OutputNotFound => write!(f, "Output not found."),
77            ObsError::DisplayCreationError(e) => write!(f, "Native error from the Windows API when creating a display: {:?}", e),
78            ObsError::OutputSaveBufferFailure(e) => write!(f, "Couldn't save output buffer: {:?}", e),
79            ObsError::SourceNotFound => write!(f, "Source not found."),
80            ObsError::SourceNotAvailable(source_name) => write!(f, "Source {} is not available. See logs or similar to check why.", source_name),
81            ObsError::InvocationError(e) => write!(f, "The obs thread couldn't be called: {:?}", e),
82            ObsError::JsonParseError => write!(f, "Failed to parse JSON data."),
83            ObsError::NoSenderError => write!(f, "Couldn't get the sender of the signal."),
84            ObsError::NoAvailableEncoders => write!(f, "No available encoders found."),
85            ObsError::OutputPauseFailure(s) => write!(f, "Output failed to pause. Error is {:?}", s),
86            ObsError::LockError(e) => write!(f, "Error locking a mutex or RwLock: {:?}", e),
87            ObsError::Unexpected(e) => write!(f, "Unexpected error: {:?}", e),
88            ObsError::EncoderActive => write!(f, "Encoder is still active, stop the attached output before proceeding"),
89            ObsError::StringConversionError => write!(f, "Error converting a string between Rust and OBS"),
90            ObsError::PlatformInitError(e) => write!(f, "Error during platform-specific initialization: {}", e),
91            ObsError::InvalidOperation(e) => write!(f, "Invalid operation: {}", e),
92        }
93    }
94}
95
96impl std::error::Error for ObsError {}