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

500 status error from stencil handlebar-helpers forEach in array.js #509

Closed
shanemac10 opened this issue Aug 13, 2019 · 6 comments
Closed

Comments

@shanemac10
Copy link

shanemac10 commented Aug 13, 2019

Expected behavior

Stencil handlebar-helpers array helpers.forEach will not crash if passed an undefined value for "array" parameter.

node_modules/@bigcommerce/stencil-cli/node_modules/handlebars-helpers/lib/array.js:200:19

Actual behavior

Server crash creates 500 error for client.

Here is the console logs from a crash

Debug: internal, implementation, error 
    TypeError: Uncaught error: Cannot read property 'length' of undefined
    at Object.helpers.forEach (/Users/shanemccurdy/.npm-global/lib/node_modules/@bigcommerce/stencil-cli/node_modules/handlebars-helpers/lib/array.js:200:19)

Steps to reproduce behavior

Pass in an undefined value into {{#forEach ...}} in a handlebars template.


I have been trying to track this down for days, as I have customer with some random products that cause 500 errors when you try to visit the product page. Somewhere in a template, (possibly a custom template, which I had no hand in creating, btw) a {{#forEach ...}} is being called on an undefined value. The logging is not verbose enough to tell which template has the culprit on my end, but I took a look at the helper, and easily fixed it locally. You have a couple ways you could address this and I'll let you be the judge of which approach you use, but this little issue should not cause a 500.

My local fix was to add an empty array as a default parameter, but you may choose to do a fast-fail...
helpers.forEach = function(array=[], options) {

Screen Shot 2019-08-13 at 4 24 55 PM

PS - Is there a way to overwrite the helper on my end, that will make it to the store on deploy (stencil push)?

@karen-white
Copy link
Contributor

Thanks for reporting this @shanemac10 ! It looks like the same issue has been opened on the Handlebars helper library that we import {{#forEach}} from: helpers/handlebars-helpers#313

It would be good to understand where you're encountering an undefined array value in the page context. Could you let us know which data causes the crash?

@shanemac10
Copy link
Author

Thanks @karen-white! All I know is that this is occurring on one particular product in our store at this time... https://walltools.com/usg-sheetrock-6-classic-stainless-steel-joint-knife/

I don't have access to the DB or (to my knowledge) see the raw data object for the products.

It was happening on multiple URLs, now only this one is left. All the URLs that it happened to seemed to be product listings that were copied from similar products; ie - There are 3", 4", 5", and 6" versions of the same product, and they make the first one for 3", then copy it for the rest and just change the number out.

(I can go into further detail on my saga if it would help, but I though I'd save you from a tl:dr)

From what I have deduced, I wonder that when a product is copied its source product maybe has an empty array and that is being copied over as undefined, or skipped entirely. Or that perhaps a save event sub-process that assigns one of the values was interrupted, crashed, who knows?

@karen-white
Copy link
Contributor

Thank you @shanemac10 ! I took a look at the page context on that product, and nothing seems unusual, although it would be good to know which array was being referenced when the error occurred. Thanks again for reporting this--we'll discuss the best way to handle the exception in Stencil and keep you posted :)

@shanemac10
Copy link
Author

@karen-white Just FYI - the URL works now, no 500 error. It seems that these bad URLs magically work after you try to fix them and just walk away for a day. Maybe it takes some time for client updates to products listings to fully propagate due to some kind of load balancing or deferred processes on the back end? I don't know. This is not directly related to this #509 issue, but I thought you should at least be aware of what's happening. Cheers!

@karen-white
Copy link
Contributor

Strange, thanks for the update! Not sure what would have changed--catalog updates should be propagated immediately. Glad it is working now though!

If you're able to submit your change to {{forEach}} as a PR on https://github.com/helpers/handlebars-helpers, we can get your fix into Stencil. Once your PR is merged, if you open an issue on https://github.com/bigcommerce/paper-handlebars, we can pull your changes in. Nice work identifying that issue and putting together a fix!

@junedkazi
Copy link
Contributor

Since the issue is resolved I am going to close it.

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

No branches or pull requests

3 participants