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

Fix: do not fail and exit properly on errors #4

Closed
wants to merge 3 commits into from
Closed

Fix: do not fail and exit properly on errors #4

wants to merge 3 commits into from

Conversation

JPeer264
Copy link
Contributor

Closes #3

Screenshot 2019-08-15 at 10 42 55

Copy link
Owner

@lukechilds lukechilds left a comment

Choose a reason for hiding this comment

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

Thanks for working on this.

The code can be made much more modular and simpler by throwing an error in the failing functions and then wrapping the existing code in a try/catch that properly handles that error.

index.js Outdated
@@ -5,46 +5,69 @@ const ora = require('ora');
const chalk = require('chalk');
const prettyMs = require('pretty-ms');

const getMetaTag = (html, property) => {
const getMetaTag = (html, property, spinner) => {
Copy link
Owner

@lukechilds lukechilds Aug 15, 2019

Choose a reason for hiding this comment

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

Same as with getMetaTagContent.

index.js Outdated
};

const getMetaTagContent = metaTagHtml => {
const getMetaTagContent = (metaTagHtml, property, spinner) => {
Copy link
Owner

@lukechilds lukechilds Aug 15, 2019

Choose a reason for hiding this comment

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

We shouldn't be passing property/spinner through here and manipulating the spinner inside the funtion. Instead the function should throw/return false and then handle that result elsewhere in the code.

};

module.exports = bundler => {
bundler.on('buildEnd', async () => {
if (process.env.NODE_ENV !== 'production') {
return;
}
console.log('');
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, good catch

Copy link
Owner

@lukechilds lukechilds left a comment

Choose a reason for hiding this comment

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

Thanks for working on this.

The code can be made much more modular and simpler by throwing an error in the failing functions and then wrapping the existing code in a try/catch that properly handles that error.

@JPeer264
Copy link
Contributor Author

I wrapped now the code in a try/catch - This handles just one error at a time though. You think that is good like that?

@JPeer264
Copy link
Contributor Author

@lukechilds ping

Copy link
Owner

@lukechilds lukechilds left a comment

Choose a reason for hiding this comment

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

@JPeer264 sorry for the delay.

I wrapped now the code in a try/catch - This handles just one error at a time though. You think that is good like that?

Yes that's perfect. If an error is encountered we should fail early. It's not normally a good idea to keep on working on something if something's gone wrong.

index.js Outdated
@@ -7,14 +7,24 @@ const prettyMs = require('pretty-ms');

const getMetaTag = (html, property) => {
const regex = new RegExp(`<meta[^>]*property=["|']${property}["|'][^>]*>`, 'i');
const executedRegex = regex.exec(html);
Copy link
Owner

Choose a reason for hiding this comment

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

I think results is a better name for this variable.

index.js Outdated
};

const getMetaTagContent = metaTagHtml => {
const getMetaTagContent = (metaTagHtml, property) => {
Copy link
Owner

Choose a reason for hiding this comment

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

Still no need to be passing property in here.

We can grab it from the metaTagHtml. Or even just dump the entire contents of metaTagHtml to the console. It's probably not a lot of text.


const end = Date.now();
const symbol = hasErrors ? chalk.red('✖') : '✨ ';
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need a space in '✖' here to keep the style consistent with the '✨ ' symbol?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case it looks better without a space, as spinner.fail does not have a space either:

Screenshot 2020-03-19 at 17 20 38

index.js Outdated

const end = Date.now();
const symbol = hasErrors ? chalk.red('✖') : '✨ ';
const text = hasErrors ?
chalk.red('Failed to fix og:image link. Please fix the above error.') :
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe remove the Please fix the above error. text here.

This could be triggered by fs.writeFileSync and it might not make sense to say fix whatever the output of that is.

Just Failed to fix og:image link. should be fine. If there is a clear error above it the user will know that's what needs attention.

@JPeer264
Copy link
Contributor Author

@lukechilds I updated the PR.

lukechilds added a commit that referenced this pull request Mar 20, 2020
@lukechilds
Copy link
Owner

Thanks @JPeer264.

Closed in 6d70ba0.

@lukechilds lukechilds closed this Mar 20, 2020
@JPeer264 JPeer264 deleted the feat/error-handling branch March 20, 2020 06:14
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.

UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of null
2 participants