1use std::{
4 error::Error as ErrorTrait,
5 ffi::{CString, NulError},
6 fmt::Display,
7 num::TryFromIntError,
8};
9
10use crate::{
11 boxed::ZBox,
12 exception::PhpException,
13 ffi::php_error_docref,
14 flags::{ClassFlags, DataType, ErrorType, ZvalTypeFlags},
15 types::ZendObject,
16};
17
18pub type Result<T, E = Error> = std::result::Result<T, E>;
20
21#[derive(Debug)]
24#[non_exhaustive]
25pub enum Error {
26 IncorrectArguments(usize, usize),
32 ZvalConversion(DataType),
36 UnknownDatatype(u32),
40 InvalidTypeToDatatype(ZvalTypeFlags),
46 InvalidScope,
49 InvalidPointer,
52 InvalidProperty,
54 InvalidCString,
57 InvalidUtf8,
59 Callable,
61 Object,
63 InvalidException(ClassFlags),
65 IntegerOverflow,
67 Exception(ZBox<ZendObject>),
69 StreamWrapperRegistrationFailure,
71 StreamWrapperUnregistrationFailure,
73 SapiWriteUnavailable,
75 LazyObjectFailed,
77}
78
79impl Display for Error {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 match self {
82 Error::IncorrectArguments(n, expected) => write!(
83 f,
84 "Expected at least {expected} arguments, got {n} arguments."
85 ),
86 Error::ZvalConversion(ty) => write!(
87 f,
88 "Could not convert Zval from type {ty} into primitive type."
89 ),
90 Error::UnknownDatatype(dt) => write!(f, "Unknown datatype {dt}."),
91 Error::InvalidTypeToDatatype(dt) => {
92 write!(f, "Type flags did not contain a datatype: {dt:?}")
93 }
94 Error::InvalidScope => write!(f, "Invalid scope."),
95 Error::InvalidPointer => write!(f, "Invalid pointer."),
96 Error::InvalidProperty => write!(f, "Property does not exist on object."),
97 Error::InvalidCString => write!(
98 f,
99 "String given contains NUL-bytes which cannot be present in a C string."
100 ),
101 Error::InvalidUtf8 => write!(f, "Invalid Utf8 byte sequence."),
102 Error::Callable => write!(f, "Could not call given function."),
103 Error::Object => write!(f, "An object was expected."),
104 Error::InvalidException(flags) => {
105 write!(f, "Invalid exception type was thrown: {flags:?}")
106 }
107 Error::IntegerOverflow => {
108 write!(f, "Converting integer arguments resulted in an overflow.")
109 }
110 Error::Exception(e) => write!(f, "Exception was thrown: {e:?}"),
111 Error::StreamWrapperRegistrationFailure => {
112 write!(f, "A failure occurred while registering the stream wrapper")
113 }
114 Error::StreamWrapperUnregistrationFailure => {
115 write!(
116 f,
117 "A failure occurred while unregistering the stream wrapper"
118 )
119 }
120 Error::SapiWriteUnavailable => {
121 write!(f, "The SAPI write function is not available")
122 }
123 Error::LazyObjectFailed => {
124 write!(f, "Failed to make the object lazy")
125 }
126 }
127 }
128}
129
130impl ErrorTrait for Error {}
131
132impl From<NulError> for Error {
133 fn from(_: NulError) -> Self {
134 Self::InvalidCString
135 }
136}
137
138impl From<TryFromIntError> for Error {
139 fn from(_value: TryFromIntError) -> Self {
140 Self::IntegerOverflow
141 }
142}
143
144impl From<Error> for PhpException {
145 fn from(err: Error) -> Self {
146 Self::default(err.to_string())
147 }
148}
149
150pub fn php_error(type_: &ErrorType, message: &str) {
158 let Ok(c_string) = CString::new(message) else {
159 return;
160 };
161
162 unsafe {
163 php_error_docref(
164 std::ptr::null(),
165 type_.bits().try_into().expect("Error type flags overflown"),
166 c_string.as_ptr(),
167 );
168 }
169}