Application Error
NameOfTheDragon opened this issue · comments
Looks like it's crashing quite often:
2020-12-14T16:08:26.360267+00:00 app[web.1]: /app/routes/get.js:63
2020-12-14T16:08:26.360276+00:00 app[web.1]: if (user.copyright) {
2020-12-14T16:08:26.360278+00:00 app[web.1]: ^
2020-12-14T16:08:26.360278+00:00 app[web.1]:
2020-12-14T16:08:26.360279+00:00 app[web.1]: TypeError: Cannot read property 'copyright' of undefined
2020-12-14T16:08:26.360280+00:00 app[web.1]: at module.exports (/app/routes/get.js:63:12)
2020-12-14T16:08:26.360281+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2020-12-14T16:08:26.360282+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2020-12-14T16:08:26.360283+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2020-12-14T16:08:26.360283+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2020-12-14T16:08:26.360284+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2020-12-14T16:08:26.360284+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:354:14)
2020-12-14T16:08:26.360284+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:365:14)
2020-12-14T16:08:26.360285+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:410:3)
2020-12-14T16:08:26.360285+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
@Richienb didn't you do a refactor recently?
@remy I did one three months ago.
This issue only happens periodically - I am unable to reproduce it consistently.
I (think I) found the problem: It's all to do with a Javascript quirk.
Here we assign response.locals.user
by extending itself with the result of a promise.
mit-license/middleware/load-user.js
Lines 17 to 20 in aa280a6
Notice that we await it within the assignment. What do you think response.locals.user
looks like while the promise is still pending?
It actually temporarily sets it to what is essentially undefined
.
To fix it, we just need to await the data before the assignment:
mit-license/middleware/load-user.js
Lines 17 to 22 in 6d916f4
I've just set up a quick uptime page so we can track if this change did anything.
cname lookup?
Sorry, we're crossing messages.
That await won't return the user object until the promise has settled (so the refactor here shouldn't make any difference).
The user object is definitely undefined. If it were set to a promise it would pass the test for user.cooyright
(because we can do prop lookups on a promise - regardless of whether the promise has settled or not).
It seems to be working again now.
The last recorded downtime is on February 11, 2021. I believe this means the issue is now resolved.