@@ -2,7 +2,6 @@ import Vue from 'vue'
2
2
import i18n from '@vue-storefront/i18n'
3
3
import isNaN from 'lodash-es/isNaN'
4
4
import isUndefined from 'lodash-es/isUndefined'
5
- import toString from 'lodash-es/toString'
6
5
import fetch from 'isomorphic-fetch'
7
6
import * as localForage from 'localforage'
8
7
import rootStore from '@vue-storefront/core/store'
@@ -16,6 +15,7 @@ import { processURLAddress } from '@vue-storefront/core/helpers'
16
15
import { serial } from '@vue-storefront/core/helpers'
17
16
import config from 'config'
18
17
import { onlineHelper } from '@vue-storefront/core/helpers'
18
+ import { hasResponseError , getResponseMessage , getResponseCode } from '@vue-storefront/core/lib/sync/helpers'
19
19
20
20
const AUTO_REFRESH_MAX_ATTEMPTS = 20
21
21
@@ -33,7 +33,7 @@ function _sleep (time) {
33
33
}
34
34
35
35
function _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
37
37
Logger . log ( 'Waiting for rootStore.state.userTokenInvalidateLock to release for ' + task . url , 'sync' ) ( )
38
38
_sleep ( 1000 ) . then ( ( ) => {
39
39
Logger . log ( 'Another try for rootStore.state.userTokenInvalidateLock for ' + task . url , 'sync' ) ( )
@@ -42,7 +42,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
42
42
return // return but not resolve
43
43
} else if ( rootStore . state . userTokenInvalidateLock < 0 ) {
44
44
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' ) } ) ( )
46
46
return
47
47
} else {
48
48
if ( rootStore . state . userTokenInvalidated ) {
@@ -73,9 +73,9 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
73
73
}
74
74
} ) . then ( ( jsonResponse ) => {
75
75
if ( jsonResponse ) {
76
- const responseCode = parseInt ( jsonResponse . code )
76
+ const responseCode = getResponseCode ( jsonResponse )
77
77
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
79
79
Logger . error ( 'Invalid token - need to be revalidated' + currentToken + task . url + rootStore . state . userTokenInvalidateLock , 'sync' ) ( )
80
80
if ( isNaN ( rootStore . state . userTokenInvalidateAttemptsCount ) || isUndefined ( rootStore . state . userTokenInvalidateAttemptsCount ) ) rootStore . state . userTokenInvalidateAttemptsCount = 0
81
81
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
128
128
}
129
129
}
130
130
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 ) {
134
132
rootStore . dispatch ( 'notification/spawnNotification' , {
135
133
type : 'error' ,
136
- message : i18n . t ( message ) ,
134
+ message : i18n . t ( getResponseMessage ( jsonResponse ) ) ,
137
135
action1 : { label : i18n . t ( 'OK' ) }
138
136
} )
139
137
}
0 commit comments