diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-08 22:14:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-08 22:14:12 +0900 |
| commit | f4cad2123b2af0de72bda4ce039e16e74f163f4e (patch) | |
| tree | 21803308c5ff41e23c9d3b117433eea16b4ff663 /crates/shirabe/src/plugin | |
| parent | 0209f63210e5b547b5c6b73367bb80ea86c255ec (diff) | |
| download | php-shirabe-f4cad2123b2af0de72bda4ce039e16e74f163f4e.tar.gz php-shirabe-f4cad2123b2af0de72bda4ce039e16e74f163f4e.tar.zst php-shirabe-f4cad2123b2af0de72bda4ce039e16e74f163f4e.zip | |
feat(php-shim): give ported exceptions PHP's class hierarchy
Ported exceptions were flat structs reached with `downcast_ref`, so
Composer's `catch (\RuntimeException $e)` only matched the exact leaf
type and `get_class($e)` had nothing to report. Each exception now
embeds an instance of the class it extends and travels inside an
`AnyThrowable`; `Catch::catch`/`catch_mut` walk that chain, and
`PhpClass::php_class_name` yields the PHP FQCN.
Dropping the `std::error::Error` impls from the exception types leaves
`AnyThrowable` as the only route into an `anyhow::Error`, so the walk
cannot be bypassed. A `no_exception_downcast` linter catches the
`downcast::<X>()` calls that would now silently answer `None`.
Three sites change behavior as a result: the `TransportException`
exit-code override reaches `MaxFileSizeExceededException`, the
`catch (\LogicException)` in findSimilar() reaches its subclasses, and
rendered exception titles carry the real class name rather than a
guess. `get_class_err()` is no longer a `todo!()`, which re-enables
FilesystemRepositoryTest::testCorruptedRepositoryFile.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/plugin')
| -rw-r--r-- | crates/shirabe/src/plugin/php_plugin_proxy.rs | 118 | ||||
| -rw-r--r-- | crates/shirabe/src/plugin/plugin_blocked_exception.rs | 14 | ||||
| -rw-r--r-- | crates/shirabe/src/plugin/plugin_manager.rs | 84 |
3 files changed, 86 insertions, 130 deletions
diff --git a/crates/shirabe/src/plugin/php_plugin_proxy.rs b/crates/shirabe/src/plugin/php_plugin_proxy.rs index 686c05bd..af9d04db 100644 --- a/crates/shirabe/src/plugin/php_plugin_proxy.rs +++ b/crates/shirabe/src/plugin/php_plugin_proxy.rs @@ -2057,10 +2057,9 @@ impl PhpPluginProxy { Ok(_) => Ok(()), // TODO(plugin): the original exception class is collapsed to RuntimeException on // this side of the boundary. - Err(throw) => Err(anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: throw.message, - code: throw.code, - })), + Err(throw) => { + Err(shirabe_php_shim::RuntimeException::with_code(throw.message, throw.code).into()) + } } } @@ -2080,10 +2079,9 @@ impl PhpPluginProxy { )?; match outcome { Ok(value) => Ok(value.to_php_mixed()?), - Err(throw) => Err(anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: throw.message, - code: throw.code, - })), + Err(throw) => { + Err(shirabe_php_shim::RuntimeException::with_code(throw.message, throw.code).into()) + } } } } @@ -2160,10 +2158,11 @@ impl EventSubscriberInterface for PhpPluginProxy { // TODO(plugin): the original exception class is collapsed to RuntimeException on // this side of the boundary. Err(throw) => { - return Err(anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: throw.message, - code: throw.code, - })); + return Err(shirabe_php_shim::RuntimeException::with_code( + throw.message, + throw.code, + ) + .into()); } }; decode_subscribed_events(&self.class, value) @@ -2191,10 +2190,11 @@ impl Capable for PhpPluginProxy { // TODO(plugin): the original exception class is collapsed to RuntimeException on // this side of the boundary. Err(throw) => { - return Err(anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: throw.message, - code: throw.code, - })); + return Err(shirabe_php_shim::RuntimeException::with_code( + throw.message, + throw.code, + ) + .into()); } }; // PHP: `(array) $plugin->getCapabilities()` — the interface declares no return type, @@ -2289,12 +2289,10 @@ fn decode_listener_priority( } fn subscribed_events_shape_error(class: &str, value: &PluginValue) -> anyhow::Error { - anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: format!( - "{class}::getSubscribedEvents() returned an unsupported shape over RPC: {value:?}" - ), - code: 0, - }) + shirabe_php_shim::RuntimeException::new(format!( + "{class}::getSubscribedEvents() returned an unsupported shape over RPC: {value:?}" + )) + .into() } impl Drop for PhpPluginProxy { @@ -2407,13 +2405,11 @@ impl PhpInstallerProxy { } fn unsupported_shape(&self, method: &str, value: &PluginValue) -> anyhow::Error { - anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: format!( - "{}::{method}() returned an unsupported shape over RPC: {value:?}", - self.handle.class - ), - code: 0, - }) + shirabe_php_shim::RuntimeException::new(format!( + "{}::{method}() returned an unsupported shape over RPC: {value:?}", + self.handle.class + )) + .into() } } @@ -2632,15 +2628,11 @@ impl CommandProvider for PhpCommandProviderProxy { PluginValue::List(items) => items, PluginValue::Array(map) => map.into_values().collect(), _ => { - return Err(anyhow::anyhow!( - shirabe_php_shim::UnexpectedValueException { - message: format!( - "Plugin capability {} failed to return an array from getCommands", - self.handle.class - ), - code: 0, - } - )); + return Err(shirabe_php_shim::UnexpectedValueException::new(format!( + "Plugin capability {} failed to return an array from getCommands", + self.handle.class + )) + .into()); } }; let mut commands: Vec<std::rc::Rc<std::cell::RefCell<dyn BaseCommand>>> = Vec::new(); @@ -2661,13 +2653,10 @@ impl CommandProvider for PhpCommandProviderProxy { } fn invalid_command_error(capability: &PhpObjHandle) -> anyhow::Error { - anyhow::anyhow!(shirabe_php_shim::UnexpectedValueException { - message: format!( - "Plugin capability {} returned an invalid value, we expected an array of Composer\\Command\\BaseCommand objects", - capability.class - ), - code: 0, - }) + shirabe_php_shim::UnexpectedValueException::new(format!( + "Plugin capability {} returned an invalid value, we expected an array of Composer\\Command\\BaseCommand objects", + capability.class + )).into() } impl Drop for PhpCommandProviderProxy { @@ -2820,12 +2809,11 @@ impl PhpConsoleApplicationContext { let app = match value { PluginValue::PhpHandle(app) => app, other => { - return Err(anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: format!( + return Err(shirabe_php_shim::RuntimeException::new( + format!( "__shirabe_console_application_boot returned an unsupported shape over RPC: {other:?}" - ), - code: 0, - })); + ) + ).into()); } }; *self.app.borrow_mut() = Some(app.clone()); @@ -3058,13 +3046,11 @@ impl PhpCommandProxy { method: &str, value: &PluginValue, ) -> anyhow::Error { - anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: format!( - "{}::{method}() returned an unsupported shape over RPC: {value:?}", - handle.class - ), - code: 0, - }) + shirabe_php_shim::RuntimeException::new(format!( + "{}::{method}() returned an unsupported shape over RPC: {value:?}", + handle.class + )) + .into() } } @@ -3081,14 +3067,11 @@ impl Command for PhpCommandProxy { ) -> anyhow::Result<i64> { let context = CONSOLE_APP_CONTEXT .with(|slot| slot.borrow().clone()) - .ok_or_else(|| { - anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: format!( + .ok_or_else(|| -> anyhow::Error { + shirabe_php_shim::RuntimeException::new(format!( "cannot run plugin-provided command {}: no worker-side console application context was published", self.handle.class - ), - code: 0, - }) + )).into() })?; let app = context.booted_app()?; let input_line = input.borrow().__to_string(); @@ -3110,13 +3093,12 @@ impl Command for PhpCommandProxy { ) -> anyhow::Result<i64> { // `run` above never reaches this template hook; a direct call would bypass the // worker-side binding, so it stays an explicit error. - Err(anyhow::anyhow!(shirabe_php_shim::RuntimeException { - message: format!( + Err(shirabe_php_shim::RuntimeException::new( + format!( "plugin-provided command {} executes in the PHP worker through run(); execute() must not be called directly", self.handle.class - ), - code: 0, - })) + ) + ).into()) } fn is_proxy_command(&self) -> bool { diff --git a/crates/shirabe/src/plugin/plugin_blocked_exception.rs b/crates/shirabe/src/plugin/plugin_blocked_exception.rs index fcf52ace..b6a6070a 100644 --- a/crates/shirabe/src/plugin/plugin_blocked_exception.rs +++ b/crates/shirabe/src/plugin/plugin_blocked_exception.rs @@ -8,14 +8,12 @@ pub struct PluginBlockedException(pub UnexpectedValueException); impl PluginBlockedException { pub fn new(message: String) -> Self { - Self(UnexpectedValueException { message, code: 0 }) + Self(UnexpectedValueException::new(message)) } } -impl std::fmt::Display for PluginBlockedException { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} - -impl std::error::Error for PluginBlockedException {} +shirabe_php_shim::impl_php_exception!( + PluginBlockedException, + 0, + r"Composer\Plugin\PluginBlockedException" +); diff --git a/crates/shirabe/src/plugin/plugin_manager.rs b/crates/shirabe/src/plugin/plugin_manager.rs index 18239ce9..c68708a5 100644 --- a/crates/shirabe/src/plugin/plugin_manager.rs +++ b/crates/shirabe/src/plugin/plugin_manager.rs @@ -234,10 +234,7 @@ impl PluginManager { let requires_composer = match requires_composer { Some(r) => r, None => { - return Err(RuntimeException { - message: format!("Plugin {} is missing a require statement for a version of the composer-plugin-api package.", package.get_name()), - code: 0, - }.into()); + return Err(RuntimeException::new(format!("Plugin {} is missing a require statement for a version of the composer-plugin-api package.", package.get_name())).into()); } }; @@ -316,10 +313,7 @@ impl PluginManager { _ => false, }; if class_is_empty { - return Err(UnexpectedValueException { - message: format!("Error while installing {}, composer-plugin packages should have a class defined in their extra key to be usable.", package.get_pretty_name()), - code: 0, - }.into()); + return Err(UnexpectedValueException::new(format!("Error while installing {}, composer-plugin packages should have a class defined in their extra key to be usable.", package.get_pretty_name())).into()); } // PHP: is_array($extra['class']) ? $extra['class'] : [$extra['class']] — an associative // array iterates its values too, and a non-string entry reaches class_exists() where it @@ -470,14 +464,11 @@ impl PluginManager { if old_installer_plugin { if !self.php_runtime_is_a(&class, "Composer\\Installer\\InstallerInterface")? { - return Err(RuntimeException { - message: format!( - "Could not activate plugin \"{}\" as \"{}\" does not implement Composer\\Installer\\InstallerInterface", - package.get_name(), - class - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Could not activate plugin \"{}\" as \"{}\" does not implement Composer\\Installer\\InstallerInterface", + package.get_name(), + class + )) .into()); } self.io.write_error(&format!( @@ -511,14 +502,11 @@ impl PluginManager { .push(PluginOrInstaller::Installer(installer)); } else if self.php_runtime_class_exists(&class, true)? { if !self.php_runtime_is_a(&class, "Composer\\Plugin\\PluginInterface")? { - return Err(RuntimeException { - message: format!( - "Could not activate plugin \"{}\" as \"{}\" does not implement Composer\\Plugin\\PluginInterface", - package.get_name(), - class - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Could not activate plugin \"{}\" as \"{}\" does not implement Composer\\Plugin\\PluginInterface", + package.get_name(), + class + )) .into()); } let handle = self.php_runtime_new_object(&class)?; @@ -534,14 +522,11 @@ impl PluginManager { .or_default() .push(PluginOrInstaller::Plugin(plugin)); } else if fail_on_missing_classes { - return Err(UnexpectedValueException { - message: format!( - "Plugin {} could not be initialized, class not found: {}", - package.get_name(), - class - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Plugin {} could not be initialized, class not found: {}", + package.get_name(), + class + )) .into()); } } @@ -1033,14 +1018,11 @@ impl PluginManager { // || !trim(...)). Once the first branch has declined, a present key always fails one // of the three disjuncts, so a present key unconditionally throws here. if let Some(value) = capabilities.get(capability) { - return Err(UnexpectedValueException { - message: format!( - "Plugin {} provided invalid capability class name(s), got {}", - plugin.get_class_name(), - var_export(value, true) - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Plugin {} provided invalid capability class name(s), got {}", + plugin.get_class_name(), + var_export(value, true) + )) .into()); } @@ -1066,14 +1048,11 @@ impl PluginManager { Some(&mut PluginRpcDispatcher::default()), ))?; if !matches!(exists, PluginValue::Bool(true)) { - return Err(RuntimeException { - message: format!( - "Cannot instantiate Capability, as class {} from plugin {} does not exist.", - capability_class, - plugin.get_class_name() - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Cannot instantiate Capability, as class {} from plugin {} does not exist.", + capability_class, + plugin.get_class_name() + )) .into()); } @@ -1116,12 +1095,9 @@ impl PluginManager { if !php_is_a(&handle, "Composer\\Plugin\\Capability\\Capability")? || !php_is_a(&handle, capability_class_name)? { - return Err(RuntimeException { - message: format!( - "Class {capability_class} must implement both Composer\\Plugin\\Capability\\Capability and {capability_class_name}." - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Class {capability_class} must implement both Composer\\Plugin\\Capability\\Capability and {capability_class_name}." + )) .into()); } |
