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

Avoid panics #392

Merged
merged 8 commits into from
Dec 29, 2018
Merged

Avoid panics #392

merged 8 commits into from
Dec 29, 2018

Conversation

elegaanz
Copy link
Member

@elegaanz elegaanz commented Dec 26, 2018

  • Use Result as much as possible
  • Display errors instead of panicking

TODO (maybe in another PR? this one is already quite big):

  • Find a way to merge Ructe/ErrorPage types, so that we can have routes returning Result<X, ErrorPage> instead of panicking when we have an Error
  • Display more details about the error, to make it easier to debug

(sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)

@elegaanz elegaanz added C: Enhancement New feature or request S: Ready for review This PR is ready to be reviewed A: Backend Code running on the server labels Dec 26, 2018
@trinity-1686a
Copy link
Contributor

(sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)

I guess it wasn't fun to write either

@trinity-1686a trinity-1686a self-requested a review December 26, 2018 21:02
Copy link
Contributor

@igalic igalic left a 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");
Copy link
Contributor

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?

Copy link
Member Author

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, ()> {
Copy link
Contributor

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()

Copy link
Member Author

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)
Copy link
Contributor

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! 👍

plume-models/src/blogs.rs Show resolved Hide resolved
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())
Copy link
Contributor

Choose a reason for hiding this comment

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

neat!

plume-models/src/comments.rs Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Dec 27, 2018

Codecov Report

Merging #392 into master will increase coverage by 0.08%.
The diff coverage is 19.5%.

@@            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

trinity-1686a
trinity-1686a previously approved these changes Dec 28, 2018
Copy link
Contributor

@trinity-1686a trinity-1686a left a 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

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> {
Copy link
Contributor

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?

Copy link
Member Author

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?

Copy link
Contributor

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)

Copy link
Member Author

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. 👍

}

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> {
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but #392 (comment)

@@ -138,7 +130,7 @@ impl Instance {
open_registrations: bool,
short_description: SafeString,
long_description: SafeString,
) {
) -> Result<usize> {
Copy link
Contributor

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?

),
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(
Copy link
Contributor

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

}

pub fn delete(&self, conn: &Connection) {
pub fn delete(&self, conn: &Connection) -> Result<usize> {
Copy link
Contributor

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

}

pub fn delete(&self, conn: &Connection, searcher: &Searcher) {
pub fn delete(&self, conn: &Connection, searcher: &Searcher) -> Result<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

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

same about Result<()>

}

pub fn grant_admin_rights(&self, conn: &Connection) {
pub fn grant_admin_rights(&self, conn: &Connection) -> Result<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same about Result<()>

}

pub fn revoke_admin_rights(&self, conn: &Connection) {
pub fn revoke_admin_rights(&self, conn: &Connection) -> Result<usize> {
Copy link
Contributor

@trinity-1686a trinity-1686a Dec 28, 2018

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:
Copy link
Contributor

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?

Copy link
Contributor

@trinity-1686a trinity-1686a left a comment

Choose a reason for hiding this comment

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

👍

@elegaanz elegaanz merged commit 80a4dae into master Dec 29, 2018
@elegaanz elegaanz deleted the result branch December 29, 2018 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A: Backend Code running on the server C: Enhancement New feature or request S: Ready for review This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants