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

Fixed env variable in lib/config.js #22

Closed
wants to merge 1 commit into from

Conversation

shaunanoordin
Copy link
Member

Bugfix: 'env' variable in config.js was returning invalid values.

Symptoms:

Analysis:

  • We've detected that the env variable in lib/config.js doesn't always have one the valid values, i.e. 'production', 'staging' or 'cam'.
  • In certain environments, the value of env is 'development' or something similarly invalid. This is the result of different Node setups whent this line is called:
var envFromShell = process.env.NODE_ENV;
  • This will cause a chain reaction with the rest of the code...
//...
var envFromBrowser = locationMatch(/\W?env=(\w+)/);
var envFromShell = process.env.NODE_ENV;
var env = envFromBrowser || envFromShell || DEFAULT_ENV;

module.exports = {
  host: hostFromBrowser || hostFromShell || API_HOSTS[env],  //env can be invalid here
  clientAppID: appFromBrowser || appFromShell || API_APPLICATION_IDS[env],  //and here
  talkHost: talkFromBrowser || talkFromShell || TALK_HOSTS[env],  //etc
  sugarHost: sugarFromBrowser || sugarFromShell || SUGAR_HOSTS[env],
};
//...

Actions:

  • Recommendation: add additional fallbacks or a value whitelist so the env var cannot be invalid.
  • This PR adds API_HOSTS[DEFAULT_ENV](and similar) to the module.exports block as a fallback.

@brian-c , can you please take a look at this? Also: thanks to @rogerhutchings for shaping this fix.

Expectiaton: the `env` variable can be one of three possible values:
'production', 'staging' or 'cam'.

Reality: Due to the nature of the environment the code runs in, this line...
```
var envFromShell = process.env.NODE_ENV;
```
...can return any number of different values, from 'staging' to 'development',
etc.

This will cause issues when we have...
```
var env = envFromBrowser || envFromShell || DEFAULT_ENV;
```
...as `env` will now take on an invalid value.

Fix: See the code.
@brian-c
Copy link
Contributor

brian-c commented Feb 3, 2016

Seems reasonable, but would it actually be safer to explicitly throw an error if we don't recognize the environment?

Where is it getting set to "development" on your machine? Is that a default somewhere?

@shaunanoordin
Copy link
Member Author

Unfortunately, I'm unable to determine what's setting my NODE_ENV variable to 'development', but I know it's the default for my system. (One month old Mac with a fresh dev environment.) @rogerhutchings mentioned other cases where NODE_ENV was set to 'undefined' - as in a 9-character string - so the wonky variable value may not be an isolated problem. (However, I'll need access to way more systems to test this out.)

I see the merits in explicitly throwing an error, but at the moment, my experience when encountering this issue threw me for a loop. The error I received in my console was Uncaught SugarClient.host is not defined, which I would never have guessed would relate to the Panoptes Client if Roger didn't share his knowledge on the matter.

This PR implements a failsafe fallback, but I can imagine an alternative that implements a... fail-unsafe? faildangerous? failhard?... sanity check with better debug info.

if (!env.match(/^(production|staging|cam)$/)) { 
  throw 'Panoptes Client Error: Invalid Environment, try setting your NODE_ENV to something that's not "'+envFromShell+'" or add "?env=staging" to your browser's URL.';
}

I'm happy with any solution as long as Panoptes Client-using devs don't go "Bwuh?" when they have an invalid NODE_ENV.

Coming to cinemas this summer: Fail Hard, starring Bruce Willis

@brian-c
Copy link
Contributor

brian-c commented Feb 3, 2016

Yeah, I think an error is the way to go. Probably leave out the query param hack though, it's for quick switching, not a long-term solution.

@brian-c
Copy link
Contributor

brian-c commented Feb 5, 2016

Closed by #23.

@brian-c brian-c closed this Feb 5, 2016
@brian-c brian-c deleted the fix-env-var-on-config branch February 5, 2016 18:02
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.

2 participants