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/downloader/file_downloader.rs | |
| 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/downloader/file_downloader.rs')
| -rw-r--r-- | crates/shirabe/src/downloader/file_downloader.rs | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/crates/shirabe/src/downloader/file_downloader.rs b/crates/shirabe/src/downloader/file_downloader.rs index cdcf750e..9b86ee56 100644 --- a/crates/shirabe/src/downloader/file_downloader.rs +++ b/crates/shirabe/src/downloader/file_downloader.rs @@ -25,6 +25,7 @@ use crate::util::Silencer; use crate::util::Url as UrlUtil; use crate::util::sync_executor; use indexmap::IndexMap; +use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ DIRECTORY_SEPARATOR, InvalidArgumentException, PATHINFO_BASENAME, PATHINFO_EXTENSION, PHP_URL_PATH, PhpMixed, RuntimeException, UnexpectedValueException, array_search, file_exists, @@ -359,11 +360,9 @@ impl FileDownloader { url: &str, ) -> anyhow::Result<String> { if !shirabe_php_shim::extension_loaded("openssl") && Some(0) == strpos(url, "https:") { - return Err(RuntimeException { - message: "You must enable the openssl extension to download files via https" - .to_string(), - code: 0, - } + return Err(RuntimeException::new( + "You must enable the openssl extension to download files via https".to_string(), + ) .into()); } @@ -400,10 +399,9 @@ impl DownloaderInterface for FileDownloader { output: bool, ) -> anyhow::Result<Option<PhpMixed>> { if package.get_dist_url().is_none() { - return Err(InvalidArgumentException { - message: "The given package is missing url information".to_string(), - code: 0, - } + return Err(InvalidArgumentException::new( + "The given package is missing url information".to_string(), + ) .into()); } @@ -558,15 +556,15 @@ impl DownloaderInterface for FileDownloader { } self.clear_last_cache_write(package.clone()); - if e.downcast_ref::<IrrecoverableDownloadException>().is_some() { + if e.is_instanceof::<IrrecoverableDownloadException>() { return Err(e); } - if e.downcast_ref::<MaxFileSizeExceededException>().is_some() { + if e.is_instanceof::<MaxFileSizeExceededException>() { return Err(e); } - if let Some(te) = e.downcast_ref::<TransportException>() { + if let Some(te) = e.catch::<TransportException>() { // if we got an http response with a proper code, then requesting again will probably not help, abort if 0 != te.get_code() && !matches!(te.get_code(), 500 | 502 | 503 | 504) { @@ -592,7 +590,7 @@ impl DownloaderInterface for FileDownloader { } if !urls.is_empty() { let code = e - .downcast_ref::<TransportException>() + .catch::<TransportException>() .map_or(0, |te| te.get_code()); if self.io.borrow().is_debug() { self.io.borrow().write_error(&format!( @@ -628,13 +626,10 @@ impl DownloaderInterface for FileDownloader { // === $result->then(verify) === if !file_exists(&file_name) { - return Err(UnexpectedValueException { - message: format!( - "{} could not be saved to {}, make sure the directory is writable and you have internet connectivity", - url.base, file_name - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "{} could not be saved to {}, make sure the directory is writable and you have internet connectivity", + url.base, file_name + )) .into()); } @@ -642,13 +637,10 @@ impl DownloaderInterface for FileDownloader { && !checksum.is_empty() && hash_file("sha1", &file_name).as_deref() != Some(checksum) { - return Err(UnexpectedValueException { - message: format!( - "The checksum verification of the file failed (downloaded from {})", - url.base - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "The checksum verification of the file failed (downloaded from {})", + url.base + )) .into()); } @@ -809,10 +801,10 @@ impl DownloaderInterface for FileDownloader { } let result = Filesystem::remove_directory_async_via(&self.filesystem, path).await?; if !result { - return Err(RuntimeException { - message: format!("Could not completely delete {}, aborting.", path), - code: 0, - } + return Err(RuntimeException::new(format!( + "Could not completely delete {}, aborting.", + path + )) .into()); } |
