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/repository/repository_set.rs | 35 +++++++++---------------- 1 file changed, 13 insertions(+), 22 deletions(-) (limited to 'crates/shirabe/src/repository/repository_set.rs') diff --git a/crates/shirabe/src/repository/repository_set.rs b/crates/shirabe/src/repository/repository_set.rs index def73f14..bd57ec20 100644 --- a/crates/shirabe/src/repository/repository_set.rs +++ b/crates/shirabe/src/repository/repository_set.rs @@ -21,6 +21,7 @@ use crate::repository::LockArrayRepositoryHandle; use crate::repository::PlatformRepository; use crate::repository::{FindPackageConstraint, RepositoryInterfaceHandle}; use indexmap::IndexMap; +use shirabe_php_shim::Catch as _; use shirabe_php_shim::{LogicException, RuntimeException, ksort, strtolower}; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::MatchAllConstraint; @@ -155,10 +156,7 @@ impl RepositorySet { /// @param RepositoryInterface $repo A package repository pub fn add_repository(&mut self, repo: RepositoryInterfaceHandle) -> anyhow::Result<()> { if self.locked { - return Err(RuntimeException { - message: "Pool has already been created from this repository set, it cannot be modified anymore.".to_string(), - code: 0, - } + return Err(RuntimeException::new("Pool has already been created from this repository set, it cannot be modified anymore.".to_string()) .into()); } @@ -374,17 +372,17 @@ impl RepositorySet { Err(e) => { // PHP catches only \Composer\Downloader\TransportException; other // exceptions propagate uncaught. - if e.downcast_ref::().is_none() { + if !e.is_instanceof::() { return Err(e); } if !ignore_unreachable { return Err(e); } let message = e - .downcast_ref::() + .catch::() .unwrap() - .message - .clone(); + .get_message() + .to_string(); unreachable_repos.push(message); } } @@ -482,11 +480,9 @@ impl RepositorySet { || repo_ref.as_any().is::() }; if is_installed && !self.allow_installed_repositories { - return Err(LogicException { - message: "The pool can not accept packages from an installed repository" - .to_string(), - code: 0, - } + return Err(LogicException::new( + "The pool can not accept packages from an installed repository".to_string(), + ) .into()); } } @@ -505,11 +501,9 @@ impl RepositorySet { || repo_ref.as_any().is::() }; if is_installed && !self.allow_installed_repositories { - return Err(LogicException { - message: "The pool can not accept packages from an installed repository" - .to_string(), - code: 0, - } + return Err(LogicException::new( + "The pool can not accept packages from an installed repository".to_string(), + ) .into()); } } @@ -582,10 +576,7 @@ impl RepositorySet { let mut allowed_packages: Vec = vec![]; for package_name in &package_names { if PlatformRepository::is_platform_package(package_name) { - return Err(LogicException { - message: "createPoolForPackage(s) can not be used for platform packages, as they are never loaded by the PoolBuilder which expects them to be fixed. Use createPoolWithAllPackages or pass in a proper request with the platform packages you need fixed in it.".to_string(), - code: 0, - } + return Err(LogicException::new("createPoolForPackage(s) can not be used for platform packages, as they are never loaded by the PoolBuilder which expects them to be fixed. Use createPoolWithAllPackages or pass in a proper request with the platform packages you need fixed in it.".to_string()) .into()); } -- cgit v1.3.1-4-g156e