-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config(): Add safety falback for when running in production (#11686)
* config(): Add safety falback for when running in production Recently the data layer experienced a total failure when #11490 merged in and crashed when it failed to initialize the new middleware handler. The fact that a key was requested for which no value existed led to `config()` raising an `Error` which causes the entire middleware chain to fail to build. Now `config()` has been modified to only raise an actual `Error` when the `NODE_ENV=developement` and instead in all other environments to simply log a message to the browser console and return `undefined`. This should allow us to still catch any mistakes by the obvious message while preventing such otherwise-small errors from cascading out-of-control.
- Loading branch information
Showing
2 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import config from '../'; | ||
|
||
const NODE_ENV = process.env.NODE_ENV; | ||
const fakeKey = 'where did all the errors go?'; | ||
|
||
describe( '#config', () => { | ||
afterEach( () => process.env.NODE_ENV = NODE_ENV ); | ||
|
||
it( `should throw an error when given key doesn't exist (NODE_ENV == development)`, () => { | ||
process.env.NODE_ENV = 'development'; | ||
|
||
expect( () => config( fakeKey ) ).to.throw( ReferenceError ); | ||
} ); | ||
|
||
it( `should not throw an error when given key doesn't exist (NODE_ENV != development)`, () => { | ||
const envs = [ | ||
'client', | ||
'desktop', | ||
'horizon', | ||
'production', | ||
'stage', | ||
'test', | ||
'wpcalypso', | ||
]; | ||
|
||
envs.forEach( env => { | ||
process.env.NODE_ENV = env; | ||
|
||
expect( process.env.NODE_ENV ).to.equal( env ); | ||
expect( () => config( fakeKey ) ).to.not.throw( Error ); | ||
} ); | ||
} ); | ||
} ); |