From 1c75a89433c82bafd31552670f7a0d6c6cd69e09 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 25 Jul 2017 16:12:45 +1000 Subject: [PATCH] Throw an exception if you try to access a non-existant channel --- app/react-native/src/preview/index.js | 10 +++++++++- lib/addons/src/index.js | 10 +++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/react-native/src/preview/index.js b/app/react-native/src/preview/index.js index c5d253887083..c1e9931a3786 100644 --- a/app/react-native/src/preview/index.js +++ b/app/react-native/src/preview/index.js @@ -52,7 +52,15 @@ export default class Preview { getStorybookUI(params = {}) { return () => { let webUrl = null; - let channel = addons.getChannel(); + let channel = null; + + try { + channel = addons.getChannel(); + } catch (e) { + // getChannel throws if the channel is not defined, + // which is fine in this case (we will define it below) + } + if (params.resetStorybook || !channel) { const host = params.host || 'localhost'; diff --git a/lib/addons/src/index.js b/lib/addons/src/index.js index ee3ae408b66e..a669b37dd5e3 100644 --- a/lib/addons/src/index.js +++ b/lib/addons/src/index.js @@ -2,15 +2,19 @@ export class AddonStore { constructor() { this.loaders = {}; this.panels = {}; - // this.channel should get overwritten by setChannel if package versions are - // correct and AddonStore is a proper singleton. If not, this will be null - // (currently required by @storybook/react-native getStorybookUI) this.channel = null; this.preview = null; this.database = null; } getChannel() { + // this.channel should get overwritten by setChannel if package versions are + // correct and AddonStore is a proper singleton. If not, throw. + if (!this.channel) { + throw new Error( + 'Accessing nonexistent addons channel, see https://storybook.js.org/basics/faq/#why-is-there-no-addons-channel' + ); + } return this.channel; }