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/config/json_config_source.rs | 49 ++++++++++--------------- 1 file changed, 19 insertions(+), 30 deletions(-) (limited to 'crates/shirabe/src/config') diff --git a/crates/shirabe/src/config/json_config_source.rs b/crates/shirabe/src/config/json_config_source.rs index 53e3722b..0257ee31 100644 --- a/crates/shirabe/src/config/json_config_source.rs +++ b/crates/shirabe/src/config/json_config_source.rs @@ -7,6 +7,7 @@ use crate::json::JsonValidationException; use crate::util::Filesystem; use crate::util::Silencer; use indexmap::IndexMap; +use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ PHP_EOL, PhpMixed, RuntimeException, chmod, explode, file_get_contents, file_put_contents, implode, is_writable, @@ -32,24 +33,18 @@ impl JsonConfigSource { let contents; if self.file.borrow().exists() { if !is_writable(self.file.borrow().get_path()) { - return Err(RuntimeException { - message: format!( - "The file \"{}\" is not writable.", - self.file.borrow().get_path(), - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "The file \"{}\" is not writable.", + self.file.borrow().get_path(), + )) .into()); } if !Filesystem::is_readable(self.file.borrow().get_path()) { - return Err(RuntimeException { - message: format!( - "The file \"{}\" is not readable.", - self.file.borrow().get_path(), - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "The file \"{}\" is not readable.", + self.file.borrow().get_path(), + )) .into()); } @@ -148,19 +143,16 @@ impl JsonConfigSource { { Ok(_) => {} Err(e) => { - let Some(jve) = e.downcast_ref::() else { + let Some(jve) = e.catch::() else { return Err(e); }; // restore contents to the original state file_put_contents(self.file.borrow().get_path(), contents.as_bytes()); - return Err(RuntimeException { - message: format!( - "Failed to update composer.json with a valid format, reverting to the original content. Please report an issue to us with details (command you run and a copy of your composer.json). {}{}", - PHP_EOL, - implode(PHP_EOL, jve.get_errors()), - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "Failed to update composer.json with a valid format, reverting to the original content. Please report an issue to us with details (command you run and a copy of your composer.json). {}{}", + PHP_EOL, + implode(PHP_EOL, jve.get_errors()), + )) .into()); } } @@ -397,13 +389,10 @@ impl ConfigSourceInterface for JsonConfigSource { } } let Some(index_to_insert) = index_to_insert else { - return Err(RuntimeException { - message: format!( - "The referenced repository \"{}\" does not exist.", - reference_name, - ), - code: 0, - } + return Err(RuntimeException::new(format!( + "The referenced repository \"{}\" does not exist.", + reference_name, + )) .into()); }; -- cgit v1.3.1-4-g156e