diff --git a/src/botPage/bot/TradeEngine/Total.js b/src/botPage/bot/TradeEngine/Total.js index 355c7c6446..64a567e559 100644 --- a/src/botPage/bot/TradeEngine/Total.js +++ b/src/botPage/bot/TradeEngine/Total.js @@ -1,7 +1,7 @@ import { translate } from '../../../common/i18n'; import { roundBalance } from '../../common/tools'; import { info, notify } from '../broadcast'; -import createError from '../../common/error'; +import { createError } from '../../common/error'; import { observer as globalObserver } from '../../../common/utils/observer'; const skeleton = { diff --git a/src/botPage/bot/TradeEngine/index.js b/src/botPage/bot/TradeEngine/index.js index 9af841a0b8..aaec4f3c65 100644 --- a/src/botPage/bot/TradeEngine/index.js +++ b/src/botPage/bot/TradeEngine/index.js @@ -3,7 +3,7 @@ import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import { durationToSecond } from '../../../common/utils/tools'; import { translate } from '../../..//common/i18n'; -import createError from '../../common/error'; +import { createError } from '../../common/error'; import { doUntilDone } from '../tools'; import { expectInitArg, expectTradeOptions } from '../sanitize'; import Proposal from './Proposal'; diff --git a/src/botPage/bot/sanitize.js b/src/botPage/bot/sanitize.js index e5e07b6525..26c5244b8c 100644 --- a/src/botPage/bot/sanitize.js +++ b/src/botPage/bot/sanitize.js @@ -1,5 +1,5 @@ import { translate } from '../../common/i18n'; -import createError from '../common/error'; +import { createError } from '../common/error'; const isPositiveNumber = num => Number.isFinite(num) && num > 0; diff --git a/src/botPage/common/error.js b/src/botPage/common/error.js index 50ba49bf20..69458cfdb2 100644 --- a/src/botPage/common/error.js +++ b/src/botPage/common/error.js @@ -1,7 +1,13 @@ -const createError = (name, message) => { +import { observer as globalObserver } from '../../common/utils/observer'; +import { translate } from '../../common/i18n'; + +export const createError = (name, message) => { const e = new Error(message); e.name = name; return e; }; -export default createError; +export const createErrorAndEmit = (name, message) => { + globalObserver.emit('ui.log.warn', `${translate(message)}`); + return createError(name, message); +}; diff --git a/src/botPage/view/blockly/index.js b/src/botPage/view/blockly/index.js index ce92b40a5b..f5d9c60fd4 100644 --- a/src/botPage/view/blockly/index.js +++ b/src/botPage/view/blockly/index.js @@ -16,7 +16,7 @@ import { cleanBeforeExport, } from './utils'; import Interpreter from '../../bot/Interpreter'; -import createError from '../../common/error'; +import { createErrorAndEmit } from '../../common/error'; import { translate, xml as translateXml } from '../../../common/i18n'; import { getLanguage } from '../../../common/lang'; import { observer as globalObserver } from '../../../common/utils/observer'; @@ -315,14 +315,45 @@ export default class _Blockly { } /* eslint-disable class-methods-use-this */ load(blockStr = '', dropEvent = {}) { - let xml; + const unrecognisedMsg = () => translate('Unrecognized file format'); + + try { + const xmlDoc = new DOMParser().parseFromString(blockStr, 'application/xml'); + if (xmlDoc.getElementsByTagName('parsererror').length) { + throw new Error(); + } + } catch (err) { + throw createErrorAndEmit('FileLoad', unrecognisedMsg()); + } + + let xml; try { xml = Blockly.Xml.textToDom(blockStr); } catch (e) { - throw createError('FileLoad', translate('Unrecognized file format')); + throw createErrorAndEmit('FileLoad', unrecognisedMsg()); } + const blocklyXml = xml.querySelectorAll('block'); + + if (!blocklyXml.length) { + throw createErrorAndEmit( + 'FileLoad', + 'XML file contains unsupported elements. Please check or modify file.' + ); + } + + blocklyXml.forEach(block => { + const blockType = block.getAttribute('type'); + + if (!Object.keys(Blockly.Blocks).includes(blockType)) { + throw createErrorAndEmit( + 'FileLoad', + 'XML file contains unsupported elements. Please check or modify file' + ); + } + }); + try { if (xml.hasAttribute('collection') && xml.getAttribute('collection') === 'true') { loadBlocks(xml, dropEvent); @@ -330,7 +361,7 @@ export default class _Blockly { loadWorkspace(xml); } } catch (e) { - throw createError('FileLoad', translate('Unable to load the block file')); + throw createErrorAndEmit('FileLoad', translate('Unable to load the block file')); } } /* eslint-disable class-methods-use-this */