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) --- .../shirabe/src/repository/package_repository.rs | 25 +++++++++------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'crates/shirabe/src/repository/package_repository.rs') diff --git a/crates/shirabe/src/repository/package_repository.rs b/crates/shirabe/src/repository/package_repository.rs index 62c8c5b3..810b0e13 100644 --- a/crates/shirabe/src/repository/package_repository.rs +++ b/crates/shirabe/src/repository/package_repository.rs @@ -16,7 +16,7 @@ use crate::repository::{ }; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::Preg; -use shirabe_php_shim::{Exception, PhpMixed, RuntimeException, php_regex, var_export}; +use shirabe_php_shim::{PhpMixed, RuntimeException, php_regex, var_export}; use shirabe_semver::constraint::AnyConstraint; #[derive(Debug)] @@ -72,10 +72,7 @@ impl PackageRepository { e, shirabe_php_shim::json_encode(package).unwrap_or_default() ); - return Ok(Err(InvalidRepositoryException(Exception { - message: msg, - code: 0, - }))); + return Ok(Err(InvalidRepositoryException::new(msg))); } }; self.inner.add_package(package_loaded)?; @@ -101,7 +98,7 @@ impl PackageRepository { // skips re-initializing it. fn ensure_initialized(&self) -> anyhow::Result<()> { if !self.inner.is_initialized() { - self.initialize()?.map_err(anyhow::Error::new)?; + self.initialize()?.map_err(anyhow::Error::from)?; } Ok(()) } @@ -234,15 +231,13 @@ impl AdvisoryProviderInterface for PackageRepository { }; if !allow_partial_advisories && matches!(advisory, AnySecurityAdvisory::Partial(_)) { - return Err(anyhow::anyhow!(RuntimeException { - message: format!( - "Advisory for {} could not be loaded as a full advisory from {}\n{}", - package_name, - self.get_repo_name()?, - var_export(data, true) - ), - code: 0, - })); + return Err(RuntimeException::new(format!( + "Advisory for {} could not be loaded as a full advisory from {}\n{}", + package_name, + self.get_repo_name()?, + var_export(data, true) + )) + .into()); } if !advisory.affected_versions().matches(package_constraint) { -- cgit v1.3.1-4-g156e