From 3912c617268468a5e5e4725171f58d290ec01648 Mon Sep 17 00:00:00 2001 From: tkostuch Date: Thu, 26 Sep 2019 10:24:39 +0200 Subject: [PATCH 1/4] check silentMode the same as task.silent --- core/lib/sync/task.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/lib/sync/task.ts b/core/lib/sync/task.ts index b1484e2a76..daad47cf08 100644 --- a/core/lib/sync/task.ts +++ b/core/lib/sync/task.ts @@ -2,6 +2,7 @@ import Vue from 'vue' import i18n from '@vue-storefront/i18n' import isNaN from 'lodash-es/isNaN' import isUndefined from 'lodash-es/isUndefined' +import isObject from 'lodash-es/isObject' import toString from 'lodash-es/toString' import fetch from 'isomorphic-fetch' import * as localForage from 'localforage' @@ -128,12 +129,12 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar } } - if (!task.silent && jsonResponse.result && (typeof jsonResponse.result === 'string' || (((jsonResponse.result.result || jsonResponse.result.message) && jsonResponse.result.code !== 'ENOTFOUND') && !silentMode))) { - const message = typeof jsonResponse.result === 'string' ? jsonResponse.result : typeof jsonResponse.result.result === 'string' ? jsonResponse.result.result : jsonResponse.result.message - + const errorMessage = isObject(jsonResponse.result) ? (jsonResponse.result.result || jsonResponse.result.message) : jsonResponse.result + const errorCode = isObject(jsonResponse.result) ? jsonResponse.result.code : jsonResponse.code + if (!task.silent && !silentMode && errorMessage && errorCode !== 'ENOTFOUND') { rootStore.dispatch('notification/spawnNotification', { type: 'error', - message: i18n.t(message), + message: i18n.t(errorMessage), action1: { label: i18n.t('OK') } }) } From ce24675bae4cef5d8122a75d91978ec904975d6b Mon Sep 17 00:00:00 2001 From: tkostuch Date: Thu, 26 Sep 2019 10:55:44 +0200 Subject: [PATCH 2/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 163f44fbe1..2e74845a7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix login errors with mailchimp - @gibkigonzo (#3612) - Hydration error on homepage - @patzick (#3609) - Fix adding products with custom options - @andrzejewsky (#3597) +- check silentMode in errors on the same level as task.silent - @gibkigonzo (#3621) ### Changed / Improved From 891fe4e7545ba01c7ef09b6469412405968895d2 Mon Sep 17 00:00:00 2001 From: tkostuch Date: Thu, 26 Sep 2019 14:53:04 +0200 Subject: [PATCH 3/4] create response helpers --- core/helpers/internal.ts | 22 ++++++++++++++++++++++ core/lib/sync/task.ts | 12 +++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/core/helpers/internal.ts b/core/helpers/internal.ts index 71eb8ab624..86bdc1ca69 100644 --- a/core/helpers/internal.ts +++ b/core/helpers/internal.ts @@ -56,3 +56,25 @@ export function takeOverConsole (level = 'no-console') { intercept(methods[i]) } } + +export const hasResponseError = (jsonResponse): boolean => { + if (typeof jsonResponse.result === 'string') { + return true + } + + const hasMessage = jsonResponse.result.result || jsonResponse.result.message + + return Boolean(hasMessage) && jsonResponse.result.code !== 'ENOTFOUND' +} + +export const getResponseMessage = (jsonResponse): string => { + if (typeof jsonResponse.result === 'string') { + return jsonResponse.result + } + + if (typeof jsonResponse.result.result === 'string') { + return jsonResponse.result.result + } + + return jsonResponse.result.message +} diff --git a/core/lib/sync/task.ts b/core/lib/sync/task.ts index daad47cf08..eec744637e 100644 --- a/core/lib/sync/task.ts +++ b/core/lib/sync/task.ts @@ -1,11 +1,7 @@ -import Vue from 'vue' import i18n from '@vue-storefront/i18n' import isNaN from 'lodash-es/isNaN' import isUndefined from 'lodash-es/isUndefined' -import isObject from 'lodash-es/isObject' -import toString from 'lodash-es/toString' import fetch from 'isomorphic-fetch' -import * as localForage from 'localforage' import rootStore from '@vue-storefront/core/store' import { adjustMultistoreApiUrl, currentStoreView } from '@vue-storefront/core/lib/multistore' import EventBus from '@vue-storefront/core/compatibility/plugins/event-bus' @@ -13,12 +9,12 @@ import Task from '@vue-storefront/core/lib/sync/types/Task' import { Logger } from '@vue-storefront/core/lib/logger' import { TaskQueue } from '@vue-storefront/core/lib/sync' import * as entities from '@vue-storefront/core/lib/store/entities' -import UniversalStorage from '@vue-storefront/core/lib/store/storage' import { StorageManager } from '@vue-storefront/core/lib/storage-manager' import { processURLAddress } from '@vue-storefront/core/helpers' import { serial } from '@vue-storefront/core/helpers' import config from 'config' import { onlineHelper } from '@vue-storefront/core/helpers' +import { hasResponseError, getResponseMessage } from '@vue-storefront/core/helpers/internal' export function _prepareTask (task) { const taskId = entities.uniqueEntityId(task) // timestamp as a order id is not the best we can do but it's enough @@ -129,12 +125,10 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar } } - const errorMessage = isObject(jsonResponse.result) ? (jsonResponse.result.result || jsonResponse.result.message) : jsonResponse.result - const errorCode = isObject(jsonResponse.result) ? jsonResponse.result.code : jsonResponse.code - if (!task.silent && !silentMode && errorMessage && errorCode !== 'ENOTFOUND') { + if (!task.silent && jsonResponse.result && hasResponseError(jsonResponse) && !silentMode) { rootStore.dispatch('notification/spawnNotification', { type: 'error', - message: i18n.t(errorMessage), + message: i18n.t(getResponseMessage(jsonResponse)), action1: { label: i18n.t('OK') } }) } From 6b06985ede60f39cdd0416af62d449071fe9ca5b Mon Sep 17 00:00:00 2001 From: tkostuch Date: Thu, 26 Sep 2019 15:26:28 +0200 Subject: [PATCH 4/4] move reponse helpers functions to sync/helpers --- core/helpers/internal.ts | 22 ---------------------- core/lib/sync/helpers/index.ts | 21 +++++++++++++++++++++ core/lib/sync/task.ts | 2 +- 3 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 core/lib/sync/helpers/index.ts diff --git a/core/helpers/internal.ts b/core/helpers/internal.ts index 86bdc1ca69..71eb8ab624 100644 --- a/core/helpers/internal.ts +++ b/core/helpers/internal.ts @@ -56,25 +56,3 @@ export function takeOverConsole (level = 'no-console') { intercept(methods[i]) } } - -export const hasResponseError = (jsonResponse): boolean => { - if (typeof jsonResponse.result === 'string') { - return true - } - - const hasMessage = jsonResponse.result.result || jsonResponse.result.message - - return Boolean(hasMessage) && jsonResponse.result.code !== 'ENOTFOUND' -} - -export const getResponseMessage = (jsonResponse): string => { - if (typeof jsonResponse.result === 'string') { - return jsonResponse.result - } - - if (typeof jsonResponse.result.result === 'string') { - return jsonResponse.result.result - } - - return jsonResponse.result.message -} diff --git a/core/lib/sync/helpers/index.ts b/core/lib/sync/helpers/index.ts new file mode 100644 index 0000000000..76bac21d1e --- /dev/null +++ b/core/lib/sync/helpers/index.ts @@ -0,0 +1,21 @@ +export const hasResponseError = (jsonResponse): boolean => { + if (typeof jsonResponse.result === 'string') { + return true + } + + const hasMessage = jsonResponse.result.result || jsonResponse.result.message + + return Boolean(hasMessage) && jsonResponse.result.code !== 'ENOTFOUND' +} + +export const getResponseMessage = (jsonResponse): string => { + if (typeof jsonResponse.result === 'string') { + return jsonResponse.result + } + + if (typeof jsonResponse.result.result === 'string') { + return jsonResponse.result.result + } + + return jsonResponse.result.message +} diff --git a/core/lib/sync/task.ts b/core/lib/sync/task.ts index eec744637e..7fe74eb26f 100644 --- a/core/lib/sync/task.ts +++ b/core/lib/sync/task.ts @@ -14,7 +14,7 @@ import { processURLAddress } from '@vue-storefront/core/helpers' import { serial } from '@vue-storefront/core/helpers' import config from 'config' import { onlineHelper } from '@vue-storefront/core/helpers' -import { hasResponseError, getResponseMessage } from '@vue-storefront/core/helpers/internal' +import { hasResponseError, getResponseMessage } from '@vue-storefront/core/lib/sync/helpers' export function _prepareTask (task) { const taskId = entities.uniqueEntityId(task) // timestamp as a order id is not the best we can do but it's enough