Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace panics with results & better option types #437

Merged
merged 4 commits into from
Nov 17, 2023

Conversation

nathanmerrill
Copy link
Contributor

@nathanmerrill nathanmerrill commented Nov 13, 2023

  1. I tried to remove as many unwraps(), panics()!, etc, with Result<>. I did not go deep into the core sync logic, as I'm not as comfortable there.

  2. I move a bunch of the parsing out of the main function and into arg-specific functions. That way, clap can provide all the context needed for an invalid argument. I also replaced "String" with more specific types in the clap commands.

AwClient was the most "substantial" change here. It's new function had several points where it would panic out. Therefore, I replaced its constructor arguments with types that prevented these panics.

Instead of taking a host+port, it just takes a reqwest::Url. This prevents the panic if the url is malformed. Additionally, it would try to read the hostname() which would also panic if the hostname used non-UTF-8 characters, so the hostname is a new parameter to the function. (I did consider rewriting get_hostname() to a non-panicing version that just strips all non-UTF-8 characters from the hostname. If you prefer this, I'm happy to update)

@ErikBjare
Copy link
Member

Awesome work!

@johan-bjareholt What do you think about the aw-client change?

aw-sync/src/main.rs Outdated Show resolved Hide resolved
aw-sync/src/main.rs Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 13, 2023

Codecov Report

Attention: 50 lines in your changes are missing coverage. Please review.

Comparison is base (01107b6) 70.58% compared to head (1c9977c) 70.88%.
Report is 3 commits behind head on master.

❗ Current head 1c9977c differs from pull request most recent head 1f96798. Consider uploading reports for the commit 1f96798 to get more accurate results

Files Patch % Lines
aw-sync/src/main.rs 0.00% 24 Missing ⚠️
aw-sync/src/sync_wrapper.rs 0.00% 11 Missing ⚠️
aw-sync/src/sync.rs 0.00% 7 Missing ⚠️
aw-sync/src/dirs.rs 0.00% 6 Missing ⚠️
aw-sync/src/util.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #437      +/-   ##
==========================================
+ Coverage   70.58%   70.88%   +0.29%     
==========================================
  Files          47       47              
  Lines        2917     2902      -15     
==========================================
- Hits         2059     2057       -2     
+ Misses        858      845      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@johan-bjareholt johan-bjareholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor comments

@@ -17,7 +17,7 @@ pub use aw_models::{Bucket, BucketMetadata, Event};

pub struct AwClient {
client: reqwest::Client,
pub baseurl: String,
pub baseurl: reqwest::Url,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to expose our reqwest dependency in the API? I think we should avoid doing that.
Wouldn't it be better to have a custom error type that the Url is invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really an API? While I found a old crates.io package, this project really does not feel like a library.

That said, if you'd like to keep that distinction, then I agree, but then I think new() should return a Result, which is fine, but a bit odd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is an API, just not a very mature one :)
As you saw, it's used by aw-sync, but its also used by aw-watcher-window-wayland.

let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(120))
.build()
.unwrap();
let hostname = gethostname::gethostname().into_string().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unwrap is extremely unlikely, you would need to essentially have invalid utf-8 in your hostname to fail here.
Keep this functionality, but maybe replace the "unwrap" with an "expect" instead.

Copy link
Contributor Author

@nathanmerrill nathanmerrill Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, very unlikely, but in that unlikely event, panic-ing doesn't make sense, I think. Maybe I should just strip out UTF-8 characters instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Copy link
Member

@ErikBjare ErikBjare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good!

@ErikBjare ErikBjare merged commit dc70318 into ActivityWatch:master Nov 17, 2023
7 checks passed
@ErikBjare
Copy link
Member

Thanks for the great PR @nathanmerrill! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants