1use std::{
2 error,
3 ffi::NulError,
4 fmt::{self, Debug, Display},
5};
6
7pub trait CustomError: Display + Debug + Send + Sync + 'static {}
8
9impl<T: Display + Debug + Send + Sync + 'static> CustomError for T {}
10
11#[derive(Debug)]
13pub enum Error {
14 UninitializedField(&'static str),
16 Initialization,
18 NulByte(NulError),
20 JsEvaluation,
22 CssInjection,
24 Dispatch,
27 Custom(Box<CustomError>),
29}
30
31impl Error {
32 pub fn custom<E: CustomError>(error: E) -> Error {
34 Error::Custom(Box::new(error))
35 }
36}
37
38impl error::Error for Error {
39 fn cause(&self) -> Option<&error::Error> {
40 match self {
41 Error::NulByte(cause) => Some(cause),
42 _ => None,
43 }
44 }
45
46 #[cfg(feature = "V1_30")]
47 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
48 match self {
49 Error::NulByte(ref cause) => Some(cause),
50 _ => None,
51 }
52 }
53}
54
55impl Display for Error {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 match self {
58 Error::UninitializedField(field) => {
59 write!(f, "Required field uninitialized: {}.", field)
60 }
61 Error::Initialization => write!(f, "Webview failed to initialize."),
62 Error::NulByte(cause) => write!(f, "{}", cause),
63 Error::JsEvaluation => write!(f, "Failed to evaluate JavaScript."),
64 Error::CssInjection => write!(f, "Failed to inject CSS."),
65 Error::Dispatch => write!(
66 f,
67 "Closure could not be dispatched. WebView was likely dropped."
68 ),
69 Error::Custom(e) => write!(f, "Error: {}", e),
70 }
71 }
72}
73
74pub type WVResult<T = ()> = Result<T, Error>;
76
77impl From<NulError> for Error {
78 fn from(e: NulError) -> Error {
79 Error::NulByte(e)
80 }
81}