@@ -6,6 +6,7 @@ use crate::{
66 Error :: Other ,
77} ;
88use anyhow:: anyhow;
9+ use derive_more:: Constructor ;
910use std:: { convert:: Infallible , error, ffi:: FromBytesWithNulError , io, str:: Utf8Error } ;
1011
1112/// PHP Throwable, can cause throwing an exception when setting to [crate::values::Val].
@@ -64,6 +65,14 @@ pub enum Error {
6465 #[ error( transparent) ]
6566 #[ throwable( transparent) ]
6667 StateType ( #[ from] StateTypeError ) ,
68+
69+ #[ error( transparent) ]
70+ #[ throwable( transparent) ]
71+ CallFunction ( #[ from] CallFunctionError ) ,
72+
73+ #[ error( transparent) ]
74+ #[ throwable( transparent) ]
75+ CallMethod ( #[ from] CallMethodError ) ,
6776}
6877
6978impl Error {
@@ -74,73 +83,39 @@ impl Error {
7483 }
7584}
7685
77- #[ derive( thiserror:: Error , Debug ) ]
86+ #[ derive( Debug , thiserror:: Error , crate :: Throwable , Constructor ) ]
7887#[ error( "type error: {message}" ) ]
88+ #[ throwable( class = "TypeError" ) ]
89+ #[ throwable_crate]
7990pub struct TypeError {
8091 message : String ,
8192}
8293
83- impl TypeError {
84- pub fn new ( message : String ) -> Self {
85- Self { message }
86- }
87- }
88-
89- impl Throwable for TypeError {
90- fn class_entry ( & self ) -> & ClassEntry < ( ) > {
91- ClassEntry :: from_globals ( "TypeError" ) . unwrap ( )
92- }
93- }
94-
95- #[ derive( thiserror:: Error , Debug ) ]
94+ #[ derive( Debug , thiserror:: Error , crate :: Throwable , Constructor ) ]
9695#[ error( "Class '{class_name}' not found" ) ]
96+ #[ throwable( class = "Error" ) ]
97+ #[ throwable_crate]
9798pub struct ClassNotFoundError {
9899 class_name : String ,
99100}
100101
101- impl ClassNotFoundError {
102- pub fn new ( class_name : String ) -> Self {
103- Self { class_name }
104- }
105- }
106-
107- impl Throwable for ClassNotFoundError {
108- fn class_entry ( & self ) -> & StatelessClassEntry {
109- ClassEntry :: from_globals ( "Error" ) . unwrap ( )
110- }
111- }
112-
113- #[ derive( thiserror:: Error , Debug ) ]
102+ #[ derive( Debug , thiserror:: Error , crate :: Throwable ) ]
114103#[ error(
115104 "Actual State type in generic type parameter isn't the state type registered in the class, \
116105 please confirm the real state type, or use StatelessClassEntry"
117106) ]
107+ #[ throwable( class = "Error" ) ]
108+ #[ throwable_crate]
118109pub struct StateTypeError ;
119110
120- impl Throwable for StateTypeError {
121- fn class_entry ( & self ) -> & StatelessClassEntry {
122- ClassEntry :: from_globals ( "Error" ) . unwrap ( )
123- }
124- }
125-
126- #[ derive( thiserror:: Error , Debug ) ]
111+ #[ derive( thiserror:: Error , Debug , Constructor ) ]
127112#[ error( "{function_name}(): expects at least {expect_count} parameter(s), {given_count} given" ) ]
128113pub struct ArgumentCountError {
129114 function_name : String ,
130115 expect_count : usize ,
131116 given_count : usize ,
132117}
133118
134- impl ArgumentCountError {
135- pub fn new ( function_name : String , expect_count : usize , given_count : usize ) -> Self {
136- Self {
137- function_name,
138- expect_count,
139- given_count,
140- }
141- }
142- }
143-
144119impl Throwable for ArgumentCountError {
145120 fn class_entry ( & self ) -> & StatelessClassEntry {
146121 let class_name = if PHP_VERSION_ID >= 70100 {
@@ -151,3 +126,20 @@ impl Throwable for ArgumentCountError {
151126 ClassEntry :: from_globals ( class_name) . unwrap ( )
152127 }
153128}
129+
130+ #[ derive( Debug , thiserror:: Error , crate :: Throwable , Constructor ) ]
131+ #[ error( "Invalid call to {fn_name}" ) ]
132+ #[ throwable( class = "BadFunctionCallException" ) ]
133+ #[ throwable_crate]
134+ pub struct CallFunctionError {
135+ fn_name : String ,
136+ }
137+
138+ #[ derive( Debug , thiserror:: Error , crate :: Throwable , Constructor ) ]
139+ #[ error( "Invalid call to {class_name}::{method_name}" ) ]
140+ #[ throwable( class = "BadMethodCallException" ) ]
141+ #[ throwable_crate]
142+ pub struct CallMethodError {
143+ class_name : String ,
144+ method_name : String ,
145+ }
0 commit comments