From f4cad2123b2af0de72bda4ce039e16e74f163f4e Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 8 Aug 2026 22:14:12 +0900 Subject: 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::()` 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) --- crates/shirabe/src/downloader/download_manager.rs | 71 ++++++++++------------- 1 file changed, 31 insertions(+), 40 deletions(-) (limited to 'crates/shirabe/src/downloader/download_manager.rs') diff --git a/crates/shirabe/src/downloader/download_manager.rs b/crates/shirabe/src/downloader/download_manager.rs index 423eda28..8c10de5d 100644 --- a/crates/shirabe/src/downloader/download_manager.rs +++ b/crates/shirabe/src/downloader/download_manager.rs @@ -9,6 +9,7 @@ use crate::package::PackageInterfaceHandle; use crate::util::Filesystem; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; +use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_keys, array_reverse, array_shift, dirname, implode, in_array_strict, preg_quote, rtrim, str_replace, @@ -99,14 +100,11 @@ impl DownloadManager { ) -> anyhow::Result>> { let r#type = strtolower(r#type); if !self.downloaders.contains_key(&r#type) { - return Err(InvalidArgumentException { - message: format!( - "Unknown downloader type: {}. Available types: {}.", - r#type, - implode(", ", &array_keys(&self.downloaders)), - ), - code: 0, - } + return Err(InvalidArgumentException::new(format!( + "Unknown downloader type: {}. Available types: {}.", + r#type, + implode(", ", &array_keys(&self.downloaders)), + )) .into()); } @@ -134,28 +132,22 @@ impl DownloadManager { } else if installation_source.as_deref() == Some("source") { self.get_downloader(&package.get_source_type().unwrap_or_default())? } else { - return Err(InvalidArgumentException { - message: format!( - "Package {} does not have an installation source set", - package, - ), - code: 0, - } + return Err(InvalidArgumentException::new(format!( + "Package {} does not have an installation source set", + package, + )) .into()); }; let downloader_installation_source = downloader.borrow().get_installation_source(); if installation_source.as_deref() != Some(&downloader_installation_source) { - return Err(LogicException { - message: format!( - "Downloader \"{}\" is a {} type downloader and can not be used to download {} for package {}", - downloader.borrow().php_class_name(), - downloader_installation_source, - installation_source.unwrap_or_default(), - package, - ), - code: 0, - } + return Err(LogicException::new(format!( + "Downloader \"{}\" is a {} type downloader and can not be used to download {} for package {}", + downloader.borrow().php_class_name(), + downloader_installation_source, + installation_source.unwrap_or_default(), + package, + )) .into()); } @@ -227,19 +219,18 @@ impl DownloadManager { { Ok(r) => r, Err(e) => { - let is_runtime = e.downcast_ref::().is_some(); - let is_irrecoverable = - e.downcast_ref::().is_some(); - if is_runtime && !is_irrecoverable { + if e.is_instanceof::() + && !e.is_instanceof::() + { if sources.is_empty() { return Err(e); } let message = e - .downcast_ref::() + .catch::() .unwrap() - .message - .clone(); + .get_message() + .to_string(); self.io.write_error3( &format!( " Failed to download {} from {}: {}", @@ -352,17 +343,17 @@ impl DownloadManager { Ok(p) => return Ok(p), Err(e) => { // PHP catches only \RuntimeException; other exceptions propagate uncaught. - if e.downcast_ref::().is_none() { + if !e.is_instanceof::() { return Err(e); } if !self.io.is_interactive() { return Err(e); } let message = e - .downcast_ref::() + .catch::() .unwrap() - .message - .clone(); + .get_message() + .to_string(); self.io.write_error3( &format!(" Update failed ({})", message), true, @@ -477,10 +468,10 @@ impl DownloadManager { } if sources.is_empty() { - return Err(InvalidArgumentException { - message: format!("Package {} must have a source or dist specified", package), - code: 0, - } + return Err(InvalidArgumentException::new(format!( + "Package {} must have a source or dist specified", + package + )) .into()); } -- cgit v1.3.1-4-g156e