Skip to content

Commit e5df7dd

Browse files
Merge pull request #7 from genkgo/remove_log_futures
remove log and futures crates
2 parents 5d02a2d + 3d75de7 commit e5df7dd

File tree

6 files changed

+78
-154
lines changed

6 files changed

+78
-154
lines changed

Cargo.lock

Lines changed: 1 addition & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ authors = ["Frederik Bosch <f.bosch@genkgo.nl>"]
77

88
[dependencies]
99
notify = "6.1.1"
10-
futures = "0.3"
11-
log = "0.4.17"
12-
env_logger = "0.10.0"
1310
phper = "0.13.1"
11+
thiserror = "1.0.43"
1412

1513
[dev-dependencies]
1614
phper-test = "0.13.1"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class RecommendedWatcher
5151

5252
/**
5353
* @param callable(Event): void $handle
54+
* @throws WatchException
5455
*/
5556
public function watch(callable $handle): void;
5657
}
@@ -66,6 +67,10 @@ class Event
6667
*/
6768
public function getPaths(): array;
6869
}
70+
71+
class WatchException extends \Exception
72+
{
73+
}
6974
```
7075

7176
## Why?

src/lib.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher, EventKind, event::*};
22
use phper::{functions::Argument, modules::Module, php_get_module, values::ZVal, classes::{ClassEntity, ClassEntry, Visibility}};
33
use phper::arrays::{ZArray, InsertKey, IterKey};
4+
use phper::errors::{exception_class, Throwable};
45
use phper::objects::{StateObj};
6+
use std::error::Error;
57
use std::convert::Infallible;
68
use std::path::PathBuf;
79

@@ -13,7 +15,27 @@ pub fn get_module() -> Module {
1315
env!("CARGO_PKG_AUTHORS"),
1416
);
1517

16-
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
18+
#[derive(Debug, thiserror::Error)]
19+
#[error(transparent)]
20+
pub struct NotifyError(pub Box<dyn Error>);
21+
22+
impl NotifyError {
23+
pub fn new(e: impl Into<Box<dyn Error>>) -> Self {
24+
Self(e.into())
25+
}
26+
}
27+
28+
impl Throwable for NotifyError {
29+
fn get_class(&self) -> &ClassEntry {
30+
ClassEntry::from_globals("FsNotify\\WatchException").unwrap_or_else(|_| exception_class())
31+
}
32+
}
33+
34+
impl From<NotifyError> for phper::Error {
35+
fn from(e: NotifyError) -> Self {
36+
phper::Error::throw(e)
37+
}
38+
}
1739

1840
let mut event = ClassEntity::new("FsNotify\\Event");
1941
event.add_property("kind", Visibility::Private, ());
@@ -80,8 +102,8 @@ pub fn get_module() -> Module {
80102
false => RecursiveMode::NonRecursive,
81103
};
82104

83-
watcher.watch(&path, recursive).unwrap();
84-
log::info!("Watching: {path:?}")
105+
watcher.watch(&path, recursive)
106+
.map_err(NotifyError::new)?;
85107
}
86108

87109
for res in rx {
@@ -150,16 +172,20 @@ pub fn get_module() -> Module {
150172

151173
handler.call([ZVal::from(php_event)])?;
152174
},
153-
Err(error) => log::error!("Error: {error:?}"),
175+
Err(error) => return Err(NotifyError::new(error).into()),
154176
}
155177
}
156178

157179
Ok::<_, phper::Error>(())
158180
})
159181
.argument(Argument::by_val("handle"));
160182

183+
let mut watch_exception = ClassEntity::new("FsNotify\\WatchException");
184+
watch_exception.extends(exception_class);
185+
161186
module.add_class(watcher);
162187
module.add_class(event);
188+
module.add_class(watch_exception);
163189

164190
module
165191
}

tests/integration.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use phper_test::{cli::test_long_term_php_script_with_condition, utils::get_lib_path};
1+
use phper_test::{cli::test_long_term_php_script_with_condition, cli::test_php_scripts_with_condition, utils::get_lib_path};
22
use std::{
33
env,
44
path::{Path, PathBuf},
@@ -35,4 +35,31 @@ fn test_recommended_watcher() {
3535
let mut buf = String::new();
3636
tmpfile.read_to_string(&mut buf).unwrap();
3737
assert_eq!("notice", buf);
38+
}
39+
40+
#[test]
41+
fn test_watch_exception() {
42+
test_php_scripts_with_condition(
43+
get_lib_path(
44+
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
45+
.join("target"),
46+
"php_ext_fs_notify",
47+
),
48+
&[
49+
(&Path::new(env!("CARGO_MANIFEST_DIR"))
50+
.join("tests")
51+
.join("php")
52+
.join("test_exception.php"),
53+
&|output| {
54+
println!("{:?}", output.status.code());
55+
match output.status.code() {
56+
Some(0) => {
57+
assert_eq!("caught exception", String::from_utf8(output.stdout.clone()).unwrap());
58+
true
59+
},
60+
_ => false,
61+
}
62+
}),
63+
],
64+
);
3865
}

tests/php/test_exception.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
ini_set("display_errors", "On");
4+
ini_set("display_startup_errors", "On");
5+
error_reporting(-1);
6+
7+
try {
8+
$watcher = new FsNotify\RecommendedWatcher();
9+
$watcher->add(__DIR__ . '/unknown');
10+
$watcher->watch(fn () => null);
11+
} catch (FsNotify\WatchException) {
12+
echo "caught exception";
13+
}

0 commit comments

Comments
 (0)