-
Notifications
You must be signed in to change notification settings - Fork 138
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
Avoid panics #392
Avoid panics #392
Conversation
I guess it wasn't fun to write either |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first pass, not done yet
@@ -120,7 +120,7 @@ pub fn broadcast<S: sign::Signer, A: Activity, T: inbox::WithInbox + Actor>( | |||
|
|||
let mut act = serde_json::to_value(act).expect("activity_pub::broadcast: serialization error"); | |||
act["@context"] = context(); | |||
let signed = act.sign(sender); | |||
let signed = act.sign(sender).expect("activity_pub::broadcast: signature error"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i thought we're trying to avoid panics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahah, right… It actually doesn't add a panic, we just panic "outside" of the function instead of "inside" because it wouldn't make much sense to return a Result
from broadcast
as it is always the main function of its thread.
@@ -105,7 +105,7 @@ pub fn headers() -> HeaderMap { | |||
headers | |||
} | |||
|
|||
pub fn signature<S: Signer>(signer: &S, headers: &HeaderMap) -> HeaderValue { | |||
pub fn signature<S: Signer>(signer: &S, headers: &HeaderMap) -> Result<HeaderValue, ()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you briefly explain this return signature?
maybe then I'll also understand the above expect()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It says that it can either success with a HeaderValue
, or fail with ()
, so we don't have much details about the error, we just know it failed, but it is probably enough to know that something failed in this function (or maybe not?)
.and_then(|instance| Blog::find_by_name(conn, actor, instance.id)) | ||
.or_else(|_| Blog::fetch_from_webfinger(conn, fqn)) | ||
} else { // local blog | ||
Blog::find_local(conn, actor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow! that's some really nice refactoring! 👍
note.object_props | ||
.set_tag_link_vec( | ||
mentions | ||
.into_iter() | ||
.map(|m| Mention::build_activity(conn, &m)) | ||
.filter_map(|m| Mention::build_activity(conn, &m).ok()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat!
Codecov Report
@@ Coverage Diff @@
## master #392 +/- ##
==========================================
+ Coverage 27.76% 27.85% +0.08%
==========================================
Files 63 63
Lines 6302 7260 +958
==========================================
+ Hits 1750 2022 +272
- Misses 4552 5238 +686 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems good.
Some Result could/should be Result<()> instead.
The ?
syntax is really helpful with error handling
plume-models/src/blogs.rs
Outdated
pub fn delete(&self, conn: &Connection, searcher: &Searcher) { | ||
for post in Post::get_for_blog(conn, &self) { | ||
post.delete(&(conn, searcher)); | ||
pub fn delete(&self, conn: &Connection, searcher: &Searcher) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could see a use for knowing how many posts were deleted, and at first at assumed it's what it was. But after re-reading it's telling how many blogs where deleted, so the Ok(_) will only be 1 any, am I right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this result is not used anywhere, it just to have the right return type without having to .map(|_| ())
, but maybe it's misleading?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is, even more because we did not document any function (nor what they return)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll change it then. 👍
plume-models/src/instance.rs
Outdated
} | ||
|
||
insert!(instances, NewInstance); | ||
get!(instances); | ||
find_by!(instances, find_by_domain, public_domain as &str); | ||
|
||
pub fn toggle_block(&self, conn: &Connection) { | ||
pub fn toggle_block(&self, conn: &Connection) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not mistaken, the usize is going to be how many entries were affected, which is one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but #392 (comment)
plume-models/src/instance.rs
Outdated
@@ -138,7 +130,7 @@ impl Instance { | |||
open_registrations: bool, | |||
short_description: SafeString, | |||
long_description: SafeString, | |||
) { | |||
) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not mistaken, the usize is going to be how many entries were affected, which is one?
plume-models/src/posts.rs
Outdated
), | ||
published: true, | ||
license: license, | ||
// FIXME: This is wrong: with this logic, we may use the display URL as the AP ID. We need two different fields | ||
ap_url: article.object_props.url_string().unwrap_or_else(|_| | ||
ap_url: article.object_props.url_string().unwrap_or( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if there are other places where unwrap_or_else(|_| ..)
was replaced with unwrap_or(..)
, if so this comment also apply to them. I think it would be better to or_else(|_| ..)?
, I think you already did it at other places
plume-models/src/tags.rs
Outdated
} | ||
|
||
pub fn delete(&self, conn: &Connection) { | ||
pub fn delete(&self, conn: &Connection) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same about returning Result<()> because it will be 1
plume-models/src/users.rs
Outdated
} | ||
|
||
pub fn delete(&self, conn: &Connection, searcher: &Searcher) { | ||
pub fn delete(&self, conn: &Connection, searcher: &Searcher) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same about Result<()>
plume-models/src/users.rs
Outdated
} | ||
|
||
pub fn grant_admin_rights(&self, conn: &Connection) { | ||
pub fn grant_admin_rights(&self, conn: &Connection) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same about Result<()>
plume-models/src/users.rs
Outdated
} | ||
|
||
pub fn revoke_admin_rights(&self, conn: &Connection) { | ||
pub fn revoke_admin_rights(&self, conn: &Connection) -> Result<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same about Result<()>
src/main.rs
Outdated
let searcher = match UnmanagedSearcher::open(&"search_index") { | ||
Err(Error::Search(e)) => match e { | ||
SearcherError::WriteLockAcquisitionError => panic!( | ||
r#"Your search index is locked. Plume can't start. To fix this issue, run: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have made this message from the start. Maybe add something about making sure no other instance of plume is already running here?
Just Result<()>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Result
as much as possibleTODO (maybe in another PR? this one is already quite big):
Result<X, ErrorPage>
instead of panicking when we have anError
(sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)