lmarkus / Kraken_Example_Date_Format_Helper

A simple application that shows how to write and use a custom dustjs helper for date formatting
8 stars 4 forks source link

Odd missing context error after clearing browser cache, cookies, data #3

Closed mikesparr closed 10 years ago

mikesparr commented 10 years ago

Can others reproduce this issue?

My test setup (mainly w/ example apps from KrakenJS site):

What I did:

Error:

$ npm start

> my-app@0.1.0 start /Users/mike/Work/my-app/src
> node index.js

Listening on 8000
db connection open
Fri, 18 Apr 2014 08:53:39 GMT uncaughtException Cannot read property 'context' of undefined
TypeError: Cannot read property 'context' of undefined
    at Object.dust.helpers.formatDate (/Users/mike/Work/my-app/src/lib/helper-formatDate.js:21:79)
    at Chunk.helper (/Users/mike/Work/my-app/src/node_modules/dustjs-linkedin/lib/dust.js:555:29)
    at body_2 (undefined:1:787)
    at Chunk.block (/Users/mike/Work/my-app/src/node_modules/dustjs-linkedin/lib/dust.js:513:12)
    at body_0 (undefined:1:1355)
    at /Users/mike/Work/my-app/src/node_modules/adaro/lib/patch/index.js:85:33
    at /Users/mike/Work/my-app/src/node_modules/adaro/lib/reader/js.js:39:13
    at /Users/mike/Work/my-app/src/node_modules/makara/lib/view/dust.js:46:13
    at ParseStream.<anonymous> (/Users/mike/Work/my-app/src/node_modules/makara/node_modules/findatag/index.js:51:13)
    at ParseStream.EventEmitter.emit (events.js:117:20)

npm ERR! my-app@0.1.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the my-app@0.1.0 start script.
npm ERR! This is most likely a problem with the my-app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node index.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls my-app
npm ERR! There is likely additional logging output above.
npm ERR! System Darwin 13.1.0
npm ERR! command "node" "/usr/local/bin/npm" "start"
npm ERR! cwd /Users/mike/Work/my-app/src
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.6
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/mike/Work/my-app/src/npm-debug.log
npm ERR! not ok code 0

My temp fix (hack) I saw this with my other custom helpers and added the following:

        //Dig the current language out of the context, or go to the fallback.
        var lang = fallbackLang;
        if (!typeof context === 'undefined') {
            lang = (context.stack && context.stack.head && context.stack.head.context && context.stack.head.context.locality) 
                || (context.stack && context.stack.tail && context.stack.tail.head.context && context.stack.tail.head.context.locality)
                || (context.stack && context.stack.tail && context.stack.tail.tail && context.stack.tail.tail.head.context && context.stack.tail.tail.head.context.locality)
                || fallbackLang;
        };

I think this is a 'hack' solution, however, and doesn't solve issue that for some reason on browser cookie/cache/data clear it breaks context and dust not passing context to helpers. I'm on Mac OSX Mavericks w/ versions seen in stack dump above and another engineer was able to recreate the same issue on Chrome as I did on Firefox, Chrome, and Safari.

To get working I had to start app with fallbackLang and then leverage the i18n_Example /set-language and then I believe it populated the context and thereafter the tags don't appear to be failing.

lmarkus commented 10 years ago

Hi Mike, Thanks for the info. I'm inclined to leave it as is; since the next version of Kraken (Which has a looot of internal refactoring) is just around the corner.
All these examples are being refactored to use it, and will be centralized within the Kraken repo.

At the very least, I will do a check to see if the context object is defined.

Thanks again!

mikesparr commented 10 years ago

Hi,

I found the issue was in the /lib/language.js file that is added to middleware in the i18n example project. It assumes the cookie will always exist that set language and sets context. The other helper examples assume these will exist too. The solution for future examples (aside from checking for context in helpers) is to edit the language.js to the following and add fallbackLang just in case cookie doesn't yet exist or is cleared out.

'use strict';

var nconf = require('nconf');

module.exports = function () {

    return function (req, res, next) {
        //Pick up the language cookie.
        var language = req.cookies.language;

        //Set the locality for this response. The template will pick the appropriate bundle
        if (language) {
            res.locals.context = res.locals.context || {};
            res.locals.context.locality = language;
        } else {
            // get language from config and set
            var fallbackLang = nconf.get('i18n').fallback || 'en-US';
            res.locals.context = res.locals.context || {};
            res.locals.context.locality = fallbackLang.toLowerCase();
        }
        next();
    };
};

I look forward to next version of KrakenJS and appreciate any insights into breaking changes to code, if applicable, because we're still in evaluation mode and prototyping.