Struct ext_php_rs::exception::PhpException
source · pub struct PhpException { /* private fields */ }
Expand description
Represents a PHP exception which can be thrown using the throw()
function.
Primarily used to return from a Result<T, PhpException>
which can
immediately be thrown by the ext-php-rs
macro API.
There are default From
implementations for any type that implements
ToString
, so these can also be returned from these functions. You can
also implement From<T>
for your custom error type.
Implementations§
source§impl PhpException
impl PhpException
sourcepub fn new(message: String, code: i32, ex: &'static ClassEntry) -> Self
pub fn new(message: String, code: i32, ex: &'static ClassEntry) -> Self
Creates a new exception instance.
Parameters
message
- Message to contain in the exception.code
- Integer code to go inside the exception.ex
- Exception type to throw.
Examples found in repository?
src/exception.rs (line 50)
49 50 51 52 53 54 55 56 57 58 59 60
pub fn default(message: String) -> Self {
Self::new(message, 0, ce::exception())
}
/// Creates an instance of an exception from a PHP class type and a message.
///
/// # Parameters
///
/// * `message` - Message to contain in the exception.
pub fn from_class<T: RegisteredClass>(message: String) -> Self {
Self::new(message, 0, T::get_metadata().ce())
}
sourcepub fn default(message: String) -> Self
pub fn default(message: String) -> Self
Creates a new default exception instance, using the default PHP
Exception
type as the exception type, with an integer code of
zero.
Parameters
message
- Message to contain in the exception.
Examples found in repository?
More examples
src/closure.rs (line 196)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
fn invoke(&mut self, _: ArgParser, ret: &mut Zval) {
if let Err(e) = self().set_zval(ret, false) {
let _ = PhpException::default(format!("Failed to return closure result to PHP: {}", e))
.throw();
}
}
}
unsafe impl<R> PhpClosure for Box<dyn FnMut() -> R>
where
R: IntoZval,
{
fn invoke(&mut self, _: ArgParser, ret: &mut Zval) {
if let Err(e) = self().set_zval(ret, false) {
let _ = PhpException::default(format!("Failed to return closure result to PHP: {}", e))
.throw();
}
}
}
impl<R> PhpOnceClosure for Box<dyn FnOnce() -> R>
where
R: IntoZval + 'static,
{
fn into_closure(self) -> Closure {
let mut this = Some(self);
Closure::wrap(Box::new(move || {
let this = match this.take() {
Some(this) => this,
None => {
let _ = PhpException::default(
"Attempted to call `FnOnce` closure more than once.".into(),
)
.throw();
return Option::<R>::None;
}
};
Some(this())
}) as Box<dyn FnMut() -> Option<R>>)
}
src/builders/class.rs (line 176)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
extern fn constructor<T: RegisteredClass>(ex: &mut ExecuteData, _: &mut Zval) {
let ConstructorMeta { constructor, .. } = match T::CONSTRUCTOR {
Some(c) => c,
None => {
PhpException::default("You cannot instantiate this class from PHP.".into())
.throw()
.expect("Failed to throw exception when constructing class");
return;
}
};
let this = match constructor(ex) {
ConstructorResult::Ok(this) => this,
ConstructorResult::Exception(e) => {
e.throw()
.expect("Failed to throw exception while constructing class");
return;
}
ConstructorResult::ArgError => return,
};
let this_obj = match ex.get_object::<T>() {
Some(obj) => obj,
None => {
PhpException::default("Failed to retrieve reference to `this` object.".into())
.throw()
.expect("Failed to throw exception while constructing class");
return;
}
};
this_obj.initialize(this);
}
sourcepub fn from_class<T: RegisteredClass>(message: String) -> Self
pub fn from_class<T: RegisteredClass>(message: String) -> Self
Creates an instance of an exception from a PHP class type and a message.
Parameters
message
- Message to contain in the exception.
sourcepub fn throw(self) -> Result<()>
pub fn throw(self) -> Result<()>
Throws the exception, returning nothing inside a result if successful and an error otherwise.
Examples found in repository?
src/closure.rs (line 197)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
fn invoke(&mut self, _: ArgParser, ret: &mut Zval) {
if let Err(e) = self().set_zval(ret, false) {
let _ = PhpException::default(format!("Failed to return closure result to PHP: {}", e))
.throw();
}
}
}
unsafe impl<R> PhpClosure for Box<dyn FnMut() -> R>
where
R: IntoZval,
{
fn invoke(&mut self, _: ArgParser, ret: &mut Zval) {
if let Err(e) = self().set_zval(ret, false) {
let _ = PhpException::default(format!("Failed to return closure result to PHP: {}", e))
.throw();
}
}
}
impl<R> PhpOnceClosure for Box<dyn FnOnce() -> R>
where
R: IntoZval + 'static,
{
fn into_closure(self) -> Closure {
let mut this = Some(self);
Closure::wrap(Box::new(move || {
let this = match this.take() {
Some(this) => this,
None => {
let _ = PhpException::default(
"Attempted to call `FnOnce` closure more than once.".into(),
)
.throw();
return Option::<R>::None;
}
};
Some(this())
}) as Box<dyn FnMut() -> Option<R>>)
}
More examples
src/builders/class.rs (line 177)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
extern fn constructor<T: RegisteredClass>(ex: &mut ExecuteData, _: &mut Zval) {
let ConstructorMeta { constructor, .. } = match T::CONSTRUCTOR {
Some(c) => c,
None => {
PhpException::default("You cannot instantiate this class from PHP.".into())
.throw()
.expect("Failed to throw exception when constructing class");
return;
}
};
let this = match constructor(ex) {
ConstructorResult::Ok(this) => this,
ConstructorResult::Exception(e) => {
e.throw()
.expect("Failed to throw exception while constructing class");
return;
}
ConstructorResult::ArgError => return,
};
let this_obj = match ex.get_object::<T>() {
Some(obj) => obj,
None => {
PhpException::default("Failed to retrieve reference to `this` object.".into())
.throw()
.expect("Failed to throw exception while constructing class");
return;
}
};
this_obj.initialize(this);
}
src/zend/handlers.rs (line 108)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
unsafe extern "C" fn read_property<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
type_: c_int,
cache_slot: *mut *mut c_void,
rv: *mut Zval,
) -> *mut Zval {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
type_: c_int,
cache_slot: *mut *mut c_void,
rv: *mut Zval,
) -> PhpResult<*mut Zval> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let self_ = &mut **obj;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
// retval needs to be treated as initialized, so we set the type to null
let rv_mut = rv.as_mut().ok_or("Invalid return zval given")?;
rv_mut.u1.type_info = ZvalTypeFlags::Null.bits();
Ok(match prop {
Some(prop) => {
prop.get(self_, rv_mut)?;
rv
}
None => zend_std_read_property(object, member, type_, cache_slot, rv),
})
}
match internal::<T>(object, member, type_, cache_slot, rv) {
Ok(rv) => rv,
Err(e) => {
let _ = e.throw();
(*rv).set_null();
rv
}
}
}
unsafe extern "C" fn write_property<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
value: *mut Zval,
cache_slot: *mut *mut c_void,
) -> *mut Zval {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
value: *mut Zval,
cache_slot: *mut *mut c_void,
) -> PhpResult<*mut Zval> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let self_ = &mut **obj;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
let value_mut = value.as_mut().ok_or("Invalid return zval given")?;
Ok(match prop {
Some(prop) => {
prop.set(self_, value_mut)?;
value
}
None => zend_std_write_property(object, member, value, cache_slot),
})
}
match internal::<T>(object, member, value, cache_slot) {
Ok(rv) => rv,
Err(e) => {
let _ = e.throw();
value
}
}
}
unsafe extern "C" fn get_properties<T: RegisteredClass>(
object: *mut ZendObject,
) -> *mut ZendHashTable {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
props: &mut ZendHashTable,
) -> PhpResult {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let self_ = &mut **obj;
let struct_props = T::get_metadata().get_properties();
for (name, val) in struct_props {
let mut zv = Zval::new();
if val.get(self_, &mut zv).is_err() {
continue;
}
props.insert(name, zv).map_err(|e| {
format!("Failed to insert value into properties hashtable: {:?}", e)
})?;
}
Ok(())
}
let props = zend_std_get_properties(object)
.as_mut()
.or_else(|| Some(ZendHashTable::new().into_raw()))
.expect("Failed to get property hashtable");
if let Err(e) = internal::<T>(object, props) {
let _ = e.throw();
}
props
}
unsafe extern "C" fn has_property<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
has_set_exists: c_int,
cache_slot: *mut *mut c_void,
) -> c_int {
#[inline(always)]
unsafe fn internal<T: RegisteredClass>(
object: *mut ZendObject,
member: *mut ZendStr,
has_set_exists: c_int,
cache_slot: *mut *mut c_void,
) -> PhpResult<c_int> {
let obj = object
.as_mut()
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
.ok_or("Invalid object pointer given")?;
let prop_name = member
.as_ref()
.ok_or("Invalid property name pointer given")?;
let props = T::get_metadata().get_properties();
let prop = props.get(prop_name.as_str()?);
let self_ = &mut **obj;
match has_set_exists {
//
// * 0 (has) whether property exists and is not NULL
0 => {
if let Some(val) = prop {
let mut zv = Zval::new();
val.get(self_, &mut zv)?;
if !zv.is_null() {
return Ok(1);
}
}
}
//
// * 1 (set) whether property exists and is true
1 => {
if let Some(val) = prop {
let mut zv = Zval::new();
val.get(self_, &mut zv)?;
if zend_is_true(&mut zv) == 1 {
return Ok(1);
}
}
}
//
// * 2 (exists) whether property exists
2 => {
if prop.is_some() {
return Ok(1);
}
}
_ => return Err(
"Invalid value given for `has_set_exists` in struct `has_property` function."
.into(),
),
};
Ok(zend_std_has_property(
object,
member,
has_set_exists,
cache_slot,
))
}
match internal::<T>(object, member, has_set_exists, cache_slot) {
Ok(rv) => rv,
Err(e) => {
let _ = e.throw();
0
}
}
}