aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/downloader/download_manager.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-08 22:14:12 +0900
committernsfisis <nsfisis@gmail.com>2026-08-08 22:14:12 +0900
commitf4cad2123b2af0de72bda4ce039e16e74f163f4e (patch)
tree21803308c5ff41e23c9d3b117433eea16b4ff663 /crates/shirabe/src/downloader/download_manager.rs
parent0209f63210e5b547b5c6b73367bb80ea86c255ec (diff)
downloadphp-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/download_manager.rs')
-rw-r--r--crates/shirabe/src/downloader/download_manager.rs71
1 files changed, 31 insertions, 40 deletions
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<std::rc::Rc<std::cell::RefCell<dyn DownloaderInterface>>> {
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::<RuntimeException>().is_some();
- let is_irrecoverable =
- e.downcast_ref::<IrrecoverableDownloadException>().is_some();
- if is_runtime && !is_irrecoverable {
+ if e.is_instanceof::<RuntimeException>()
+ && !e.is_instanceof::<IrrecoverableDownloadException>()
+ {
if sources.is_empty() {
return Err(e);
}
let message = e
- .downcast_ref::<RuntimeException>()
+ .catch::<RuntimeException>()
.unwrap()
- .message
- .clone();
+ .get_message()
+ .to_string();
self.io.write_error3(
&format!(
" <warning>Failed to download {} from {}: {}</warning>",
@@ -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::<RuntimeException>().is_none() {
+ if !e.is_instanceof::<RuntimeException>() {
return Err(e);
}
if !self.io.is_interactive() {
return Err(e);
}
let message = e
- .downcast_ref::<RuntimeException>()
+ .catch::<RuntimeException>()
.unwrap()
- .message
- .clone();
+ .get_message()
+ .to_string();
self.io.write_error3(
&format!("<error> Update failed ({})</error>", 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());
}