aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/json/json_file.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/json/json_file.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/json/json_file.rs')
-rw-r--r--crates/shirabe/src/json/json_file.rs71
1 files changed, 29 insertions, 42 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index 1d6a75ce..2d061ced 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -11,6 +11,7 @@ use crate::util::Silencer;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_external_packages::seld::json_lint::{ParsingException, ParsingExceptionDetails};
+use shirabe_php_shim::Catch as _;
use shirabe_php_shim::{
InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,
PhpMixed, RuntimeException, UnexpectedValueException, dirname, file_exists, file_get_contents,
@@ -108,10 +109,9 @@ impl JsonFile {
io: Option<std::rc::Rc<std::cell::RefCell<dyn IOInterface>>>,
) -> anyhow::Result<Self> {
if http_downloader.is_none() && Preg::is_match(php_regex!(r"{^https?://}i"), &path) {
- return Err(InvalidArgumentException {
- message: "http urls require a HttpDownloader instance to be passed".to_string(),
- code: 0,
- }
+ return Err(InvalidArgumentException::new(
+ "http urls require a HttpDownloader instance to be passed".to_string(),
+ )
.into());
}
Ok(Self {
@@ -145,10 +145,10 @@ impl JsonFile {
.map(|s| s.to_string()))
} else {
if !Filesystem::is_readable(&self.path) {
- return Err(RuntimeException {
- message: format!("The file \"{}\" is not readable.", self.path),
- code: 0,
- }
+ return Err(RuntimeException::new(format!(
+ "The file \"{}\" is not readable.",
+ self.path
+ ))
.into());
}
if let Some(io) = &self.io
@@ -173,17 +173,13 @@ impl JsonFile {
Err(e) => {
// TransportException keeps its message verbatim; any other exception is wrapped
// with the "Could not read" prefix.
- if let Some(te) = e.downcast_ref::<TransportException>() {
- return Err(RuntimeException {
- message: te.message.clone(),
- code: 0,
- }
- .into());
- }
- return Err(RuntimeException {
- message: format!("Could not read {}\n\n{}", self.path, e),
- code: 0,
+ if let Some(te) = e.catch::<TransportException>() {
+ return Err(RuntimeException::new(te.get_message().to_string()).into());
}
+ return Err(RuntimeException::new(format!(
+ "Could not read {}\n\n{}",
+ self.path, e
+ ))
.into());
}
};
@@ -191,11 +187,7 @@ impl JsonFile {
let json = match json {
Some(j) => j,
None => {
- return Err(RuntimeException {
- message: format!("Could not read {}", self.path),
- code: 0,
- }
- .into());
+ return Err(RuntimeException::new(format!("Could not read {}", self.path)).into());
}
};
@@ -230,21 +222,18 @@ impl JsonFile {
let dir = dirname(&self.path);
if !is_dir(&dir) {
if file_exists(&dir) {
- return Err(UnexpectedValueException {
- message: format!(
- "{} exists and is not a directory.",
- realpath(&dir).unwrap_or_default(),
- ),
- code: 0,
- }
+ return Err(UnexpectedValueException::new(format!(
+ "{} exists and is not a directory.",
+ realpath(&dir).unwrap_or_default(),
+ ))
.into());
}
// PHP: @mkdir($dir, 0777, true)
if !Silencer::call(|| Ok(mkdir(&dir, 0o777, true))).unwrap_or(false) {
- return Err(UnexpectedValueException {
- message: format!("{} does not exist and could not be created.", dir),
- code: 0,
- }
+ return Err(UnexpectedValueException::new(format!(
+ "{} does not exist and could not be created.",
+ dir
+ ))
.into());
}
}
@@ -305,10 +294,10 @@ impl JsonFile {
/// @return true true on success
pub fn validate_schema(&self, schema: i64, schema_file: Option<&str>) -> anyhow::Result<bool> {
if !Filesystem::is_readable(&self.path) {
- return Err(RuntimeException {
- message: format!("The file \"{}\" is not readable.", self.path),
- code: 0,
- }
+ return Err(RuntimeException::new(format!(
+ "The file \"{}\" is not readable.",
+ self.path
+ ))
.into());
}
let content = file_get_contents(&self.path).unwrap_or_default();
@@ -446,10 +435,8 @@ impl JsonFile {
data: &T,
options: JsonEncodeOptions,
) -> anyhow::Result<String> {
- let json = json_encode_ex(data, options.to_flags()).map_err(|err| RuntimeException {
- message: format!("JSON encoding failed: {}", err),
- code: 0,
- })?;
+ let json = json_encode_ex(data, options.to_flags())
+ .map_err(|err| RuntimeException::new(format!("JSON encoding failed: {}", err)))?;
if options.pretty_print && options.indent != Self::INDENT_DEFAULT {
// Pretty printing and not using default indentation