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/package/loader/array_loader.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/package/loader/array_loader.rs')
| -rw-r--r-- | crates/shirabe/src/package/loader/array_loader.rs | 94 |
1 files changed, 36 insertions, 58 deletions
diff --git a/crates/shirabe/src/package/loader/array_loader.rs b/crates/shirabe/src/package/loader/array_loader.rs index 0940183c..2c6d3971 100644 --- a/crates/shirabe/src/package/loader/array_loader.rs +++ b/crates/shirabe/src/package/loader/array_loader.rs @@ -69,23 +69,17 @@ impl ArrayLoader { class: &str, ) -> anyhow::Result<CompleteOrRootPackage> { if !config.contains_key("name") { - return Err(UnexpectedValueException { - message: format!( - "Unknown package has no name defined ({}).", - json_encode(&PhpMixed::Array(config.clone())).unwrap_or_default() - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Unknown package has no name defined ({}).", + json_encode(&PhpMixed::Array(config.clone())).unwrap_or_default() + )) .into()); } if !config.contains_key("version") || !is_scalar(config.get("version").unwrap()) { - return Err(UnexpectedValueException { - message: format!( - "Package {} has no version defined.", - config.get("name").and_then(|v| v.as_string()).unwrap_or("") - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Package {} has no version defined.", + config.get("name").and_then(|v| v.as_string()).unwrap_or("") + )) .into()); } let mut config_version = config.get("version").cloned().unwrap_or(PhpMixed::Null); @@ -118,14 +112,11 @@ impl ArrayLoader { { Ok(v) => version = v, Err(e) => { - return Err(UnexpectedValueException { - message: format!( - "Failed to normalize version for package \"{}\": {}", - config.get("name").and_then(|v| v.as_string()).unwrap_or(""), - e - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Failed to normalize version for package \"{}\": {}", + config.get("name").and_then(|v| v.as_string()).unwrap_or(""), + e + )) .into()); } } @@ -226,18 +217,14 @@ impl ArrayLoader { }) .unwrap_or(false); if !has_required { - return Err(UnexpectedValueException { - message: format!( - "Package {}'s source key should be specified as {{\"type\": ..., \"url\": ..., \"reference\": ...}},\n{} given.", - - config - .get("name") - .and_then(|v| v.as_string()) - .unwrap_or(""), - json_encode(&source).unwrap_or_default(), - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Package {}'s source key should be specified as {{\"type\": ..., \"url\": ..., \"reference\": ...}},\n{} given.", + config + .get("name") + .and_then(|v| v.as_string()) + .unwrap_or(""), + json_encode(&source).unwrap_or_default(), + )) .into()); } let source_map = source_map.unwrap(); @@ -270,18 +257,14 @@ impl ArrayLoader { .map(|m| m.contains_key("type") && m.contains_key("url")) .unwrap_or(false); if !has_required { - return Err(UnexpectedValueException { - message: format!( - "Package {}'s dist key should be specified as {{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...}},\n{} given.", - - config - .get("name") - .and_then(|v| v.as_string()) - .unwrap_or(""), - json_encode(&dist).unwrap_or_default(), - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Package {}'s dist key should be specified as {{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...}},\n{} given.", + config + .get("name") + .and_then(|v| v.as_string()) + .unwrap_or(""), + json_encode(&dist).unwrap_or_default(), + )) .into()); } let dist_map = dist_map.unwrap(); @@ -672,13 +655,10 @@ impl ArrayLoader { let parsed_constraint = match self.version_parser.parse_constraints(&constraint) { Ok(c) => c, Err(_e) => { - return Err(UnexpectedValueException { - message: format!( - "Link constraint in {} {} > {} should be a valid version constraint, got \"{}\"", - source, description, target, constraint - ), - code: 0, - } + return Err(UnexpectedValueException::new(format!( + "Link constraint in {} {} > {} should be a valid version constraint, got \"{}\"", + source, description, target, constraint + )) .into()); } }; @@ -702,11 +682,9 @@ impl ArrayLoader { config: &IndexMap<String, PhpMixed>, ) -> anyhow::Result<Option<String>> { if !config.contains_key("version") || !is_scalar(config.get("version").unwrap()) { - return Err(UnexpectedValueException { - message: "no/invalid version defined".to_string(), - code: 0, - } - .into()); + return Err( + UnexpectedValueException::new("no/invalid version defined".to_string()).into(), + ); } let mut config_version = config.get("version").cloned().unwrap_or(PhpMixed::Null); if !is_string(&config_version) { |
