aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/package
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/package
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/package')
-rw-r--r--crates/shirabe/src/package/alias_package.rs7
-rw-r--r--crates/shirabe/src/package/archiver/archivable_files_finder.rs8
-rw-r--r--crates/shirabe/src/package/archiver/archive_manager.rs16
-rw-r--r--crates/shirabe/src/package/archiver/phar_archiver.rs18
-rw-r--r--crates/shirabe/src/package/archiver/zip_archiver.rs2
-rw-r--r--crates/shirabe/src/package/loader/array_loader.rs94
-rw-r--r--crates/shirabe/src/package/loader/invalid_package_exception.rs14
-rw-r--r--crates/shirabe/src/package/loader/json_loader.rs5
-rw-r--r--crates/shirabe/src/package/loader/root_package_loader.rs30
-rw-r--r--crates/shirabe/src/package/loader/validating_array_loader.rs5
-rw-r--r--crates/shirabe/src/package/locker.rs47
-rw-r--r--crates/shirabe/src/package/package.rs7
-rw-r--r--crates/shirabe/src/package/version/version_guesser.rs7
-rw-r--r--crates/shirabe/src/package/version/version_selector.rs11
14 files changed, 108 insertions, 163 deletions
diff --git a/crates/shirabe/src/package/alias_package.rs b/crates/shirabe/src/package/alias_package.rs
index 088cd6ab..52b6efa4 100644
--- a/crates/shirabe/src/package/alias_package.rs
+++ b/crates/shirabe/src/package/alias_package.rs
@@ -435,10 +435,9 @@ impl PackageInterface for AliasPackage {
if let Some(existing) = self.repository.as_ref().and_then(|w| w.upgrade())
&& !std::rc::Rc::ptr_eq(&existing, repository.as_rc())
{
- return Err(LogicException {
- message: "A package can only be added to one repository".to_string(),
- code: 0,
- }
+ return Err(LogicException::new(
+ "A package can only be added to one repository".to_string(),
+ )
.into());
}
self.repository = Some(repository.downgrade());
diff --git a/crates/shirabe/src/package/archiver/archivable_files_finder.rs b/crates/shirabe/src/package/archiver/archivable_files_finder.rs
index ad2b41ab..f60c57f1 100644
--- a/crates/shirabe/src/package/archiver/archivable_files_finder.rs
+++ b/crates/shirabe/src/package/archiver/archivable_files_finder.rs
@@ -28,10 +28,10 @@ impl ArchivableFilesFinder {
let sources_real_path = realpath(sources);
if sources_real_path.is_none() {
- return Err(RuntimeException {
- message: format!("Could not realpath() the source directory \"{}\"", sources),
- code: 0,
- }
+ return Err(RuntimeException::new(format!(
+ "Could not realpath() the source directory \"{}\"",
+ sources
+ ))
.into());
}
let sources = fs.normalize_path(&sources_real_path.unwrap());
diff --git a/crates/shirabe/src/package/archiver/archive_manager.rs b/crates/shirabe/src/package/archiver/archive_manager.rs
index afc89f32..435843a6 100644
--- a/crates/shirabe/src/package/archiver/archive_manager.rs
+++ b/crates/shirabe/src/package/archiver/archive_manager.rs
@@ -114,10 +114,9 @@ impl ArchiveManager {
ignore_filters: bool,
) -> anyhow::Result<String> {
if format.is_empty() {
- return Err(anyhow::anyhow!(InvalidArgumentException {
- message: "Format must be specified".to_string(),
- code: 0,
- }));
+ return Err(
+ InvalidArgumentException::new("Format must be specified".to_string()).into(),
+ );
}
let mut usable_archiver_idx: Option<usize> = None;
@@ -131,10 +130,11 @@ impl ArchiveManager {
let usable_archiver_idx = match usable_archiver_idx {
Some(i) => i,
None => {
- return Err(anyhow::anyhow!(RuntimeException {
- message: format!("No archiver found to support {} format", format),
- code: 0,
- }));
+ return Err(RuntimeException::new(format!(
+ "No archiver found to support {} format",
+ format
+ ))
+ .into());
}
};
diff --git a/crates/shirabe/src/package/archiver/phar_archiver.rs b/crates/shirabe/src/package/archiver/phar_archiver.rs
index fbab8c67..0968f2a4 100644
--- a/crates/shirabe/src/package/archiver/phar_archiver.rs
+++ b/crates/shirabe/src/package/archiver/phar_archiver.rs
@@ -101,10 +101,10 @@ impl ArchiverInterface for PharArchiver {
} else if format == "tar.gz" || format == "tar.bz2" {
let compress_algo = *compress_formats.get(format.as_str()).unwrap();
if !PharData::can_compress(compress_algo) {
- return Err(RuntimeException {
- message: format!("Can not compress to {} format", format),
- code: 0,
- }
+ return Err(RuntimeException::new(format!(
+ "Can not compress to {} format",
+ format
+ ))
.into());
}
if format == "tar.gz" && function_exists("gzcompress") {
@@ -124,10 +124,10 @@ impl ArchiverInterface for PharArchiver {
if compress_formats.contains_key(format.as_str()) {
let compress_algo = *compress_formats.get(format.as_str()).unwrap();
if !PharData::can_compress(compress_algo) {
- return Err(RuntimeException {
- message: format!("Can not compress to {} format", format),
- code: 0,
- }
+ return Err(RuntimeException::new(format!(
+ "Can not compress to {} format",
+ format
+ ))
.into());
}
@@ -147,7 +147,7 @@ impl ArchiverInterface for PharArchiver {
"Could not create archive '{}' from '{}': {}",
target_outer, sources, e
);
- anyhow::anyhow!(RuntimeException { message, code: 0 })
+ RuntimeException::new(message).into()
})
}
diff --git a/crates/shirabe/src/package/archiver/zip_archiver.rs b/crates/shirabe/src/package/archiver/zip_archiver.rs
index 186d76c7..e5bb613d 100644
--- a/crates/shirabe/src/package/archiver/zip_archiver.rs
+++ b/crates/shirabe/src/package/archiver/zip_archiver.rs
@@ -111,7 +111,7 @@ impl ArchiverInterface for ZipArchiver {
sources,
zip.get_status_string()
);
- Err(RuntimeException { message, code: 0 }.into())
+ Err(RuntimeException::new(message).into())
}
fn supports(&self, format: String, _source_type: Option<String>) -> bool {
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) {
diff --git a/crates/shirabe/src/package/loader/invalid_package_exception.rs b/crates/shirabe/src/package/loader/invalid_package_exception.rs
index 2bd22c61..23251994 100644
--- a/crates/shirabe/src/package/loader/invalid_package_exception.rs
+++ b/crates/shirabe/src/package/loader/invalid_package_exception.rs
@@ -27,7 +27,7 @@ impl InvalidPackageException {
.join("\n")
);
Self {
- inner: Exception { message, code: 0 },
+ inner: Exception::new(message),
errors,
warnings,
data,
@@ -47,10 +47,8 @@ impl InvalidPackageException {
}
}
-impl std::fmt::Display for InvalidPackageException {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.inner.message)
- }
-}
-
-impl std::error::Error for InvalidPackageException {}
+shirabe_php_shim::impl_php_exception!(
+ InvalidPackageException,
+ inner,
+ r"Composer\Package\Loader\InvalidPackageException"
+);
diff --git a/crates/shirabe/src/package/loader/json_loader.rs b/crates/shirabe/src/package/loader/json_loader.rs
index c30fbe07..d9e7100a 100644
--- a/crates/shirabe/src/package/loader/json_loader.rs
+++ b/crates/shirabe/src/package/loader/json_loader.rs
@@ -34,10 +34,7 @@ impl JsonLoader {
let config: IndexMap<String, PhpMixed> = match config {
PhpMixed::Array(m) => m,
_ => {
- return Err(TypeError {
- message: "Composer\\Package\\Loader\\LoaderInterface::load(): Argument #1 ($config) must be of type array".to_string(),
- code: 0,
- }
+ return Err(TypeError::new("Composer\\Package\\Loader\\LoaderInterface::load(): Argument #1 ($config) must be of type array".to_string())
.into());
}
};
diff --git a/crates/shirabe/src/package/loader/root_package_loader.rs b/crates/shirabe/src/package/loader/root_package_loader.rs
index 284bb175..e26a7e95 100644
--- a/crates/shirabe/src/package/loader/root_package_loader.rs
+++ b/crates/shirabe/src/package/loader/root_package_loader.rs
@@ -78,10 +78,7 @@ impl RootPackageLoader {
config["name"].as_string().unwrap_or(""),
false,
) {
- return Err(anyhow::anyhow!(RuntimeException {
- message: format!("Your package name {}", err),
- code: 0,
- }));
+ return Err(RuntimeException::new(format!("Your package name {}", err)).into());
}
let mut auto_versioned = false;
@@ -197,13 +194,10 @@ impl RootPackageLoader {
let package_name = config["name"].as_string().unwrap_or("").to_string();
if links.contains_key(&package_name) {
- return Err(anyhow::anyhow!(RuntimeException {
- message: format!(
- "Root package '{}' cannot require itself in its composer.json\nDid you accidentally name your root package after an external package?",
- package_name
- ),
- code: 0,
- }));
+ return Err(RuntimeException::new(format!(
+ "Root package '{}' cannot require itself in its composer.json\nDid you accidentally name your root package after an external package?",
+ package_name
+ )).into());
}
}
}
@@ -216,10 +210,7 @@ impl RootPackageLoader {
if let Some(err) =
ValidatingArrayLoader::has_package_naming_error(link_name, true)
{
- return Err(anyhow::anyhow!(RuntimeException {
- message: format!("{}.{}", link_type, err),
- code: 0,
- }));
+ return Err(RuntimeException::new(format!("{}.{}", link_type, err)).into());
}
}
}
@@ -291,14 +282,11 @@ impl RootPackageLoader {
return {
panic!(
"{}",
- UnexpectedValueException {
- message: format!(
+ UnexpectedValueException::new(format!(
"Invalid alias definition in \"{}\": \"{}\". Aliases should be in the form \"exact-version as other-exact-version\".",
req_name, req_version
- ),
- code: 0,
- }
- .message
+ ))
+ .get_message()
)
};
}
diff --git a/crates/shirabe/src/package/loader/validating_array_loader.rs b/crates/shirabe/src/package/loader/validating_array_loader.rs
index a050f2d7..fc8eed00 100644
--- a/crates/shirabe/src/package/loader/validating_array_loader.rs
+++ b/crates/shirabe/src/package/loader/validating_array_loader.rs
@@ -1583,11 +1583,12 @@ impl LoaderInterface for ValidatingArrayLoader {
}
if !self.errors.borrow().is_empty() {
- return Err(anyhow::anyhow!(InvalidPackageException::new(
+ return Err(InvalidPackageException::new(
self.errors.borrow().clone(),
self.warnings.borrow().clone(),
config.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
- )));
+ )
+ .into());
}
let package = self.loader.load(
diff --git a/crates/shirabe/src/package/locker.rs b/crates/shirabe/src/package/locker.rs
index a4d28861..3b20704a 100644
--- a/crates/shirabe/src/package/locker.rs
+++ b/crates/shirabe/src/package/locker.rs
@@ -26,6 +26,7 @@ use crate::util::ProcessExecutor;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_external_packages::seld::json_lint::ParsingException;
+use shirabe_php_shim::Catch as _;
use shirabe_php_shim::{
DATE_RFC3339, LogicException, PhpMixed, RuntimeException, array_intersect, array_keys,
array_map, array_merge, file_get_contents, filemtime, function_exists, hash, in_array_loose,
@@ -194,10 +195,7 @@ impl Locker {
if let Some(packages_dev) = lock_data.get("packages-dev").cloned() {
locked_packages = array_merge(locked_packages, packages_dev);
} else {
- return Err(RuntimeException {
- message: "The lock file does not contain require-dev information, run install with the --no-dev option or delete it and run composer update to generate a new lock file.".to_string(),
- code: 0,
- }
+ return Err(RuntimeException::new("The lock file does not contain require-dev information, run install with the --no-dev option or delete it and run composer update to generate a new lock file.".to_string())
.into());
}
}
@@ -270,12 +268,10 @@ impl Locker {
return Ok(packages);
}
- Err(RuntimeException {
- message:
- "Your composer.lock is invalid. Run \"composer update\" to generate a new one."
- .to_string(),
- code: 0,
- }
+ Err(RuntimeException::new(
+ "Your composer.lock is invalid. Run \"composer update\" to generate a new one."
+ .to_string(),
+ )
.into())
}
@@ -438,10 +434,9 @@ impl Locker {
}
if !self.lock_file.exists() {
- return Err(LogicException {
- message: "No lockfile found. Unable to read locked packages".to_string(),
- code: 0,
- }
+ return Err(LogicException::new(
+ "No lockfile found. Unable to read locked packages".to_string(),
+ )
.into());
}
@@ -582,7 +577,7 @@ impl Locker {
let is_locked = match self.is_locked_result() {
Ok(b) => b,
Err(e) => {
- if e.downcast_ref::<ParsingException>().is_some() {
+ if e.is_instanceof::<ParsingException>() {
false
} else {
return Err(e);
@@ -641,13 +636,10 @@ impl Locker {
let contents = match contents {
Some(s) => s,
None => {
- return Err(RuntimeException {
- message: format!(
- "Unable to read {} contents to update the lock file hash.",
- composer_json.get_path()
- ),
- code: 0,
- }
+ return Err(RuntimeException::new(format!(
+ "Unable to read {} contents to update the lock file hash.",
+ composer_json.get_path()
+ ))
.into());
}
};
@@ -720,13 +712,10 @@ impl Locker {
let version = package.get_pretty_version();
if name.is_empty() || version.is_empty() {
- return Err(LogicException {
- message: format!(
- "Package \"{}\" has no version or name and can not be locked",
- package,
- ),
- code: 0,
- }
+ return Err(LogicException::new(format!(
+ "Package \"{}\" has no version or name and can not be locked",
+ package,
+ ))
.into());
}
diff --git a/crates/shirabe/src/package/package.rs b/crates/shirabe/src/package/package.rs
index bc99d08a..0f1e7d23 100644
--- a/crates/shirabe/src/package/package.rs
+++ b/crates/shirabe/src/package/package.rs
@@ -732,10 +732,9 @@ impl PackageInterface for Package {
if let Some(existing) = self.repository.as_ref().and_then(|w| w.upgrade())
&& !std::rc::Rc::ptr_eq(&existing, repository.as_rc())
{
- return Err(LogicException {
- message: "A package can only be added to one repository".to_string(),
- code: 0,
- }
+ return Err(LogicException::new(
+ "A package can only be added to one repository".to_string(),
+ )
.into());
}
self.repository = Some(repository.downgrade());
diff --git a/crates/shirabe/src/package/version/version_guesser.rs b/crates/shirabe/src/package/version/version_guesser.rs
index 98853952..1968ff11 100644
--- a/crates/shirabe/src/package/version/version_guesser.rs
+++ b/crates/shirabe/src/package/version/version_guesser.rs
@@ -750,10 +750,9 @@ impl VersionGuesser {
let version = match version {
Some(v) if !v.is_empty() => v,
_ => {
- return Err(RuntimeException {
- message: "COMPOSER_ROOT_VERSION not set or empty".to_string(),
- code: 0,
- }
+ return Err(RuntimeException::new(
+ "COMPOSER_ROOT_VERSION not set or empty".to_string(),
+ )
.into());
}
};
diff --git a/crates/shirabe/src/package/version/version_selector.rs b/crates/shirabe/src/package/version/version_selector.rs
index 84f2beef..733f342d 100644
--- a/crates/shirabe/src/package/version/version_selector.rs
+++ b/crates/shirabe/src/package/version/version_selector.rs
@@ -76,13 +76,10 @@ impl VersionSelector {
show_warnings: ShowWarnings,
) -> anyhow::Result<Option<crate::package::PackageInterfaceHandle>> {
if !base_package::STABILITIES.contains_key(preferred_stability) {
- return Err(shirabe_php_shim::UnexpectedValueException {
- message: format!(
- "Expected a valid stability name as 3rd argument, got {}",
- preferred_stability
- ),
- code: 0,
- }
+ return Err(shirabe_php_shim::UnexpectedValueException::new(format!(
+ "Expected a valid stability name as 3rd argument, got {}",
+ preferred_stability
+ ))
.into());
}