@@ -2,7 +2,6 @@ import Vue from 'vue'
22import i18n from '@vue-storefront/i18n'
33import isNaN from 'lodash-es/isNaN'
44import isUndefined from 'lodash-es/isUndefined'
5- import toString from 'lodash-es/toString'
65import fetch from 'isomorphic-fetch'
76import * as localForage from 'localforage'
87import rootStore from '@vue-storefront/core/store'
@@ -16,6 +15,7 @@ import { processURLAddress } from '@vue-storefront/core/helpers'
1615import { serial } from '@vue-storefront/core/helpers'
1716import config from 'config'
1817import { onlineHelper } from '@vue-storefront/core/helpers'
18+ import { hasResponseError , getResponseMessage , getResponseCode } from '@vue-storefront/core/lib/sync/helpers'
1919
2020const AUTO_REFRESH_MAX_ATTEMPTS = 20
2121
@@ -33,7 +33,7 @@ function _sleep (time) {
3333}
3434
3535function _internalExecute ( resolve , reject , task : Task , currentToken , currentCartId ) {
36- if ( currentToken !== null && rootStore . state . userTokenInvalidateLock > 0 ) { // invalidate lock set
36+ if ( currentToken && rootStore . state . userTokenInvalidateLock > 0 ) { // invalidate lock set
3737 Logger . log ( 'Waiting for rootStore.state.userTokenInvalidateLock to release for ' + task . url , 'sync' ) ( )
3838 _sleep ( 1000 ) . then ( ( ) => {
3939 Logger . log ( 'Another try for rootStore.state.userTokenInvalidateLock for ' + task . url , 'sync' ) ( )
@@ -42,7 +42,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
4242 return // return but not resolve
4343 } else if ( rootStore . state . userTokenInvalidateLock < 0 ) {
4444 Logger . error ( 'Aborting the network task' + task . url + rootStore . state . userTokenInvalidateLock , 'sync' ) ( )
45- resolve ( { code : 401 , message : i18n . t ( 'Error refreshing user token. User is not authorized to access the resource' ) } ) ( )
45+ resolve ( { code : 401 , result : i18n . t ( 'Error refreshing user token. User is not authorized to access the resource' ) } ) ( )
4646 return
4747 } else {
4848 if ( rootStore . state . userTokenInvalidated ) {
@@ -73,9 +73,9 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
7373 }
7474 } ) . then ( ( jsonResponse ) => {
7575 if ( jsonResponse ) {
76- const responseCode = parseInt ( jsonResponse . code )
76+ const responseCode = getResponseCode ( jsonResponse )
7777 if ( responseCode !== 200 ) {
78- if ( responseCode === 401 /** unauthorized */ && currentToken !== null ) { // the token is no longer valid, try to invalidate it
78+ if ( responseCode === 401 /** unauthorized */ && currentToken ) { // the token is no longer valid, try to invalidate it
7979 Logger . error ( 'Invalid token - need to be revalidated' + currentToken + task . url + rootStore . state . userTokenInvalidateLock , 'sync' ) ( )
8080 if ( isNaN ( rootStore . state . userTokenInvalidateAttemptsCount ) || isUndefined ( rootStore . state . userTokenInvalidateAttemptsCount ) ) rootStore . state . userTokenInvalidateAttemptsCount = 0
8181 if ( isNaN ( rootStore . state . userTokenInvalidateLock ) || isUndefined ( rootStore . state . userTokenInvalidateLock ) ) rootStore . state . userTokenInvalidateLock = 0
@@ -128,12 +128,10 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
128128 }
129129 }
130130
131- if ( ! task . silent && jsonResponse . result && ( typeof jsonResponse . result === 'string' || ( ( ( jsonResponse . result . result || jsonResponse . result . message ) && jsonResponse . result . code !== 'ENOTFOUND' ) && ! silentMode ) ) ) {
132- const message = typeof jsonResponse . result === 'string' ? jsonResponse . result : typeof jsonResponse . result . result === 'string' ? jsonResponse . result . result : jsonResponse . result . message
133-
131+ if ( ! task . silent && jsonResponse . result && hasResponseError ( jsonResponse ) && ! silentMode ) {
134132 rootStore . dispatch ( 'notification/spawnNotification' , {
135133 type : 'error' ,
136- message : i18n . t ( message ) ,
134+ message : i18n . t ( getResponseMessage ( jsonResponse ) ) ,
137135 action1 : { label : i18n . t ( 'OK' ) }
138136 } )
139137 }
0 commit comments