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

BB-672: Rewrite entity folder using async/await #980

Closed
wants to merge 10 commits into from

Conversation

allgandalf
Copy link
Contributor

Problem

BB-672:
Across the codebase we have asynchronous code written using promises, which is hard to read and to maintain and error-prone.

Solution

All this code was rewritten using the async/await syntax.

Areas of Impact

The changes were reflected in src/server/routes/entity

@allgandalf
Copy link
Contributor Author

Hey @MonkeyDo , fixed the merge conflict, i guess after the Wikipedia PR, there were some changes in that particular file so there was a conflict, anyway fixed it :)

Copy link
Contributor

@MonkeyDo MonkeyDo left a comment

Choose a reason for hiding this comment

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

Generally looks pretty good, thanks for starting on this folder!

It looks like you can apply some of the same refactors to edition.ts in the same folder; any reason why you didn't?

Comment on lines +204 to +209
const editions = await Publisher.forge({bbid: res.locals.entity.bbid})
.editions({withRelated: editionRelationsToFetch});
res.locals.entity.editions = editions.toJSON();
_setPublisherTitle(res);
res.locals.entity.editions.sort(entityRoutes.compareEntitiesByDate);
await entityRoutes.displayEntity(req, res);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be wrapped in a try/catch block in case one of these promises fails (for example the entity does not exist, etc.).

In the catch block, you can simply call the next function with the error (which is what we were doing before with .catch(next)).

propsPromise.author = data && utils.entityToOption(data.toJSON());
}
catch (err) {
log.debug(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

I will suggest that we call next in all these catch blocks, like I described above, so that we catch errors a bit better.

Currently we don't get an error, but the user won't know why the entity hasn't been added to the initial relationships:
For example, with a non existing BBID: https://bookbrainz.org/work/create?author=f1906497-5614-4793-9f51-a3870f3edcad
Page loads, but no relationship (because no author entity with that BBID); I think it would be better for the user to get an error message instead.

Should be as simple as

Suggested change
log.debug(err);
log.debug(err);
return next(err);

await render(props);
}
catch (err) {
log.debug(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, we want tot catch the error and pass it along as we did before:

Suggested change
log.debug(err);
log.debug(err);
next(err);

.catch(next);
catch (err) {
log.debug(err);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same deal here, we need to froward the error once caught with next(err);

if (entity.workSection) {
entity.workSection = await utils.parseLanguages(entity.workSection, req.app.locals.orm);
}
const props = await generateEntityProps(
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 why this was set up that way previously with makePromiseFromObject, but generateEntityProps does not return a promise, so there's no need to await it.

}
);


Copy link
Contributor

Choose a reason for hiding this comment

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

The file src/server/routes/entity/edition.ts also has the same refactor you can apply for the '/create' routes at least:
https://github.com/metabrainz/bookbrainz-site/blob/master/src/server/routes/entity/edition.ts#L278-L295

@allgandalf
Copy link
Contributor Author

Hey @MonkeyDo, thanks for the review, I'm little busy for a few weeks, but I'll surely complete this PR and get it merged :)

@MonkeyDo
Copy link
Contributor

Hi @RohanSasne !
Checking in to see if you would have some time to finish this PR, or if you would prefer to hand it off to me instead.

@Tarunmeena0901
Copy link
Contributor

hey @MonkeyDo is this PR completed or may be i can complete this one for other files as well ?

@allgandalf
Copy link
Contributor Author

hello @Tarunmeena0901, if you have the bandwidth, you can surely take this over :) sorry @MonkeyDo , have been a lot busy lately

@Tarunmeena0901
Copy link
Contributor

yeah sure ...... just want to know if monkey has taken over this PR

@MonkeyDo
Copy link
Contributor

Closing in favor of #1058

@MonkeyDo MonkeyDo closed this Feb 19, 2024
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