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/path_downloader.rs | 100 +++++++++-------------- 1 file changed, 37 insertions(+), 63 deletions(-) (limited to 'crates/shirabe/src/downloader/path_downloader.rs') diff --git a/crates/shirabe/src/downloader/path_downloader.rs b/crates/shirabe/src/downloader/path_downloader.rs index f10da72a..9449e74a 100644 --- a/crates/shirabe/src/downloader/path_downloader.rs +++ b/crates/shirabe/src/downloader/path_downloader.rs @@ -85,17 +85,14 @@ impl PathDownloader { package: PackageInterfaceHandle, path: &str, ) -> anyhow::Result { - let url = package.get_dist_url().ok_or_else(|| RuntimeException { - message: format!( + let url = package.get_dist_url().ok_or_else(|| { + RuntimeException::new(format!( "The package {} has no dist url configured, cannot install.", package.get_pretty_name() - ), - code: 0, - })?; - let real_url = realpath(&url).ok_or_else(|| RuntimeException { - message: format!("Failed to realpath {}", url), - code: 0, + )) })?; + let real_url = realpath(&url) + .ok_or_else(|| RuntimeException::new(format!("Failed to realpath {}", url)))?; if realpath(path).as_deref() == Some(&real_url) { return Ok(": Source already present".to_string()); @@ -157,10 +154,7 @@ impl PathDownloader { && !self.safe_junctions() { if !allowed_strategies.contains(&Self::STRATEGY_MIRROR) { - return Err(RuntimeException { - message: "You are on an old Windows / old PHP combo which does not allow Composer to use junctions/symlinks and this path repository has symlink:true in its options so copying is not allowed".to_string(), - code: 0, - } + return Err(RuntimeException::new("You are on an old Windows / old PHP combo which does not allow Composer to use junctions/symlinks and this path repository has symlink:true in its options so copying is not allowed".to_string()) .into()); } current_strategy = Self::STRATEGY_MIRROR; @@ -173,10 +167,7 @@ impl PathDownloader { && !function_exists("symlink") { if !allowed_strategies.contains(&Self::STRATEGY_MIRROR) { - return Err(RuntimeException { - message: "Your PHP has the symlink() function disabled which does not allow Composer to use symlinks and this path repository has symlink:true in its options so copying is not allowed".to_string(), - code: 0, - } + return Err(RuntimeException::new("Your PHP has the symlink() function disabled which does not allow Composer to use symlinks and this path repository has symlink:true in its options so copying is not allowed".to_string()) .into()); } current_strategy = Self::STRATEGY_MIRROR; @@ -243,26 +234,22 @@ impl DownloaderInterface for PathDownloader { output: bool, ) -> anyhow::Result> { let path = Filesystem::trim_trailing_slash(path); - let url = package.get_dist_url().ok_or_else(|| RuntimeException { - message: format!( + let url = package.get_dist_url().ok_or_else(|| { + RuntimeException::new(format!( "The package {} has no dist url configured, cannot download.", package.get_pretty_name() - ), - code: 0, + )) })?; let real_url = realpath(&url); if real_url.is_none() || !file_exists(real_url.as_deref().unwrap_or("")) || !is_dir(real_url.as_deref().unwrap_or("")) { - return Err(RuntimeException { - message: format!( - "Source path \"{}\" is not found for package {}", - url, - package.get_name() - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Source path \"{}\" is not found for package {}", + url, + package.get_name() + )) .into()); } let real_url = real_url.unwrap(); @@ -282,15 +269,12 @@ impl DownloaderInterface for PathDownloader { // // Please see https://github.com/composer/composer/pull/5974 and https://github.com/composer/composer/pull/6174 // for previous attempts that were shut down because they did not work well enough or introduced too many risks. - return Err(RuntimeException { - message: format!( - "Package {} cannot install to \"{}\" inside its source at \"{}\"", - package.get_name(), - realpath(&path).unwrap_or_default(), - real_url - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Package {} cannot install to \"{}\" inside its source at \"{}\"", + package.get_name(), + realpath(&path).unwrap_or_default(), + real_url + )) .into()); } @@ -316,17 +300,14 @@ impl DownloaderInterface for PathDownloader { output: bool, ) -> anyhow::Result> { let path = Filesystem::trim_trailing_slash(path); - let url = package.get_dist_url().ok_or_else(|| RuntimeException { - message: format!( + let url = package.get_dist_url().ok_or_else(|| { + RuntimeException::new(format!( "The package {} has no dist url configured, cannot install.", package.get_pretty_name() - ), - code: 0, - })?; - let real_url = realpath(&url).ok_or_else(|| RuntimeException { - message: format!("Failed to realpath {}", url), - code: 0, + )) })?; + let real_url = realpath(&url) + .ok_or_else(|| RuntimeException::new(format!("Failed to realpath {}", url)))?; if realpath(&path).as_deref() == Some(&real_url) { if output { @@ -442,13 +423,10 @@ impl DownloaderInterface for PathDownloader { current_strategy = Self::STRATEGY_MIRROR; is_fallback = true; } else { - return Err(RuntimeException { - message: format!( - "Symlink from \"{}\" to \"{}\" failed!", - real_url, path - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Symlink from \"{}\" to \"{}\" failed!", + real_url, path + )) .into()); } } @@ -537,25 +515,21 @@ impl DownloaderInterface for PathDownloader { true, io_interface::NORMAL, ); - return Err(RuntimeException { - message: format!( - "Could not reliably remove junction for package {}", - package.get_name() - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Could not reliably remove junction for package {}", + package.get_name() + )) .into()); } return Ok(None); } - let url = package.get_dist_url().ok_or_else(|| RuntimeException { - message: format!( + let url = package.get_dist_url().ok_or_else(|| { + RuntimeException::new(format!( "The package {} has no dist url configured, cannot remove.", package.get_pretty_name() - ), - code: 0, + )) })?; // ensure that the source path (dist url) is not the same as the install path, which -- cgit v1.3.1-4-g156e