From 1bcc6382aa18fdf5eabb9f26f082f65321f37bb5 Mon Sep 17 00:00:00 2001 From: jos Date: Thu, 29 Sep 2016 14:26:20 +0200 Subject: [PATCH] Handle error when switching mode --- src/TextMode.js | 5 ++--- src/TreeMode.js | 13 ++++++++++++- src/menu/ModeButton.js | 2 +- src/menu/ModeMenu.js | 11 ++++++++--- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/TextMode.js b/src/TextMode.js index 964f270..d7d0136 100644 --- a/src/TextMode.js +++ b/src/TextMode.js @@ -33,11 +33,10 @@ export default class TextMode extends Component { h('div', {class: 'jsoneditor-vertical-menu-separator'}), this.props.options.modes && h(ModeButton, { - open: this.state.modeMenuOpen, modes: this.props.options.modes, mode: this.props.mode, onMode: this.props.onMode, - onClick: this.handleShowModeMenu + onError: this.handleError }) ]), @@ -92,7 +91,7 @@ export default class TextMode extends Component { } /** @private */ - handleError (err) { + handleError = (err) => { if (this.props.options && this.props.options.onError) { this.props.options.onError(err) } diff --git a/src/TreeMode.js b/src/TreeMode.js index 2c44c6a..35e2254 100644 --- a/src/TreeMode.js +++ b/src/TreeMode.js @@ -101,7 +101,8 @@ export default class TreeMode extends Component { this.props.options.modes && h(ModeButton, { modes: this.props.options.modes, mode: this.props.mode, - onMode: this.props.onMode + onMode: this.props.onMode, + onError: this.handleError }) ]) } @@ -199,6 +200,16 @@ export default class TreeMode extends Component { this.emitOnChange (actions, revert) } + /** @private */ + handleError = (err) => { + if (this.props.options && this.props.options.onError) { + this.props.options.onError(err) + } + else { + console.error(err) + } + } + /** * Emit an onChange event when there is a listener for it. * @param {JSONPatch} patch diff --git a/src/menu/ModeButton.js b/src/menu/ModeButton.js index b9bde29..cbba1cb 100644 --- a/src/menu/ModeButton.js +++ b/src/menu/ModeButton.js @@ -12,7 +12,7 @@ export default class ModeButton extends Component { } /** - * @param {{modes: string[], mode: string, onMode: function}} props + * @param {{modes: string[], mode: string, onMode: function, onError: function}} props * @param state * @return {*} */ diff --git a/src/menu/ModeMenu.js b/src/menu/ModeMenu.js index dea0ce9..39e5598 100644 --- a/src/menu/ModeMenu.js +++ b/src/menu/ModeMenu.js @@ -3,7 +3,7 @@ import { toCapital } from '../utils/stringUtils' export default class ModeMenu extends Component { /** - * @param {{open, modes, mode, onMode}} props + * @param {{open, modes, mode, onMode, onError}} props * @param {Obect} state * @return {JSX.Element} */ @@ -15,8 +15,13 @@ export default class ModeMenu extends Component { class: 'jsoneditor-menu-button jsoneditor-type-modes' + ((mode === props.mode) ? ' jsoneditor-selected' : ''), onClick: () => { - props.onMode(mode) - this.setState({ open: false }) + try { + props.onMode(mode) + this.setState({ open: false }) + } + catch (err) { + props.onError(err) + } } }, toCapital(mode)) })