• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

extphprs / ext-php-rs / 21720380521

05 Feb 2026 04:49PM UTC coverage: 34.396% (-0.06%) from 34.453%
21720380521

Pull #668

github

web-flow
Merge 8fa76ff0b into b40709753
Pull Request #668: feat(class): Class structs can now contain other class structs #182

0 of 10 new or added lines in 3 files covered. (0.0%)

102 existing lines in 3 files now uncovered.

2083 of 6056 relevant lines covered (34.4%)

23.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/error.rs
1
//! Error and result types returned from the library functions.
2

3
use std::{
4
    error::Error as ErrorTrait,
5
    ffi::{CString, NulError},
6
    fmt::Display,
7
    num::TryFromIntError,
8
};
9

10
use crate::{
11
    boxed::ZBox,
12
    exception::PhpException,
13
    ffi::php_error_docref,
14
    flags::{ClassFlags, DataType, ErrorType, ZvalTypeFlags},
15
    types::ZendObject,
16
};
17

18
/// The main result type which is passed by the library.
19
pub type Result<T, E = Error> = std::result::Result<T, E>;
20

21
/// The main error type which is passed by the library inside the custom
22
/// [`Result`] type.
23
#[derive(Debug)]
24
#[non_exhaustive]
25
pub enum Error {
26
    /// An incorrect number of arguments was given to a PHP function.
27
    ///
28
    /// The enum carries two integers - the first representing the minimum
29
    /// number of arguments expected, and the second representing the number of
30
    /// arguments that were received.
31
    IncorrectArguments(usize, usize),
32
    /// There was an error converting a Zval into a primitive type.
33
    ///
34
    /// The enum carries the data type of the Zval.
35
    ZvalConversion(DataType),
36
    /// The type of the Zval is unknown.
37
    ///
38
    /// The enum carries the integer representation of the type of Zval.
39
    UnknownDatatype(u32),
40
    /// Attempted to convert a [`ZvalTypeFlags`] struct to a [`DataType`].
41
    /// The flags did not contain a datatype.
42
    ///
43
    /// The enum carries the flags that were attempted to be converted to a
44
    /// [`DataType`].
45
    InvalidTypeToDatatype(ZvalTypeFlags),
46
    /// The function called was called in an invalid scope (calling
47
    /// class-related functions inside of a non-class bound function).
48
    InvalidScope,
49
    /// The pointer inside a given type was invalid, either null or pointing to
50
    /// garbage.
51
    InvalidPointer,
52
    /// The given property name does not exist.
53
    InvalidProperty,
54
    /// The string could not be converted into a C-string due to the presence of
55
    /// a NUL character.
56
    InvalidCString,
57
    /// The string could not be converted into a valid Utf8 string
58
    InvalidUtf8,
59
    /// Could not call the given function.
60
    Callable,
61
    /// An object was expected.
62
    Object,
63
    /// An invalid exception type was thrown.
64
    InvalidException(ClassFlags),
65
    /// Converting integer arguments resulted in an overflow.
66
    IntegerOverflow,
67
    /// An exception was thrown in a function.
68
    Exception(ZBox<ZendObject>),
69
    /// A failure occurred while registering the stream wrapper
70
    StreamWrapperRegistrationFailure,
71
    /// A failure occurred while unregistering the stream wrapper
72
    StreamWrapperUnregistrationFailure,
73
    /// The SAPI write function is not available
74
    SapiWriteUnavailable,
75
    /// Failed to make an object lazy (PHP 8.4+)
76
    LazyObjectFailed,
77
    /// Failed to extract a [`ZendClassObject`] from a [`ZendObject`].
78
    ///
79
    /// This typically occurs when trying to convert a PHP object to a specific
80
    /// Rust type, but the object is not an instance of the expected class.
81
    ///
82
    /// [`ZendClassObject`]: crate::types::ZendClassObject
83
    /// [`ZendObject`]: crate::types::ZendObject
84
    ZendClassObjectExtraction,
85
}
86

87
impl Display for Error {
UNCOV
88
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
UNCOV
89
        match self {
×
90
            Error::IncorrectArguments(n, expected) => write!(
×
91
                f,
×
92
                "Expected at least {expected} arguments, got {n} arguments."
93
            ),
94
            Error::ZvalConversion(ty) => write!(
×
95
                f,
×
96
                "Could not convert Zval from type {ty} into primitive type."
97
            ),
98
            Error::UnknownDatatype(dt) => write!(f, "Unknown datatype {dt}."),
×
UNCOV
99
            Error::InvalidTypeToDatatype(dt) => {
×
UNCOV
100
                write!(f, "Type flags did not contain a datatype: {dt:?}")
×
101
            }
102
            Error::InvalidScope => write!(f, "Invalid scope."),
×
103
            Error::InvalidPointer => write!(f, "Invalid pointer."),
×
104
            Error::InvalidProperty => write!(f, "Property does not exist on object."),
×
105
            Error::InvalidCString => write!(
×
UNCOV
106
                f,
×
107
                "String given contains NUL-bytes which cannot be present in a C string."
108
            ),
UNCOV
109
            Error::InvalidUtf8 => write!(f, "Invalid Utf8 byte sequence."),
×
110
            Error::Callable => write!(f, "Could not call given function."),
×
UNCOV
111
            Error::Object => write!(f, "An object was expected."),
×
112
            Error::InvalidException(flags) => {
×
UNCOV
113
                write!(f, "Invalid exception type was thrown: {flags:?}")
×
114
            }
115
            Error::IntegerOverflow => {
116
                write!(f, "Converting integer arguments resulted in an overflow.")
×
117
            }
UNCOV
118
            Error::Exception(e) => write!(f, "Exception was thrown: {e:?}"),
×
119
            Error::StreamWrapperRegistrationFailure => {
UNCOV
120
                write!(f, "A failure occurred while registering the stream wrapper")
×
121
            }
122
            Error::StreamWrapperUnregistrationFailure => {
UNCOV
123
                write!(
×
124
                    f,
×
125
                    "A failure occurred while unregistering the stream wrapper"
126
                )
127
            }
128
            Error::SapiWriteUnavailable => {
UNCOV
129
                write!(f, "The SAPI write function is not available")
×
130
            }
131
            Error::LazyObjectFailed => {
UNCOV
132
                write!(f, "Failed to make the object lazy")
×
133
            }
134
            Error::ZendClassObjectExtraction => {
NEW
UNCOV
135
                write!(
×
NEW
UNCOV
136
                    f,
×
137
                    "Failed to extract ZendClassObject: object is not an instance of the expected class"
138
                )
139
            }
140
        }
141
    }
142
}
143

144
impl ErrorTrait for Error {}
145

146
impl From<NulError> for Error {
UNCOV
147
    fn from(_: NulError) -> Self {
×
UNCOV
148
        Self::InvalidCString
×
149
    }
150
}
151

152
impl From<TryFromIntError> for Error {
UNCOV
153
    fn from(_value: TryFromIntError) -> Self {
×
UNCOV
154
        Self::IntegerOverflow
×
155
    }
156
}
157

158
impl From<Error> for PhpException {
159
    fn from(err: Error) -> Self {
×
UNCOV
160
        Self::default(err.to_string())
×
161
    }
162
}
163

164
/// Trigger an error that is reported in PHP the same way `trigger_error()` is.
165
///
166
/// See specific error type descriptions at <https://www.php.net/manual/en/errorfunc.constants.php>.
167
///
168
/// # Panics
169
///
170
/// * If the error type bits exceed `i32::MAX`.
UNCOV
171
pub fn php_error(type_: &ErrorType, message: &str) {
×
UNCOV
172
    let Ok(c_string) = CString::new(message) else {
×
UNCOV
173
        return;
×
174
    };
175

176
    unsafe {
177
        php_error_docref(
UNCOV
178
            std::ptr::null(),
×
UNCOV
179
            type_.bits().try_into().expect("Error type flags overflown"),
×
UNCOV
180
            c_string.as_ptr(),
×
181
        );
182
    }
183
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc