Skip to content

Commit e6580d9

Browse files
authored
Merge pull request #77 from contentstack/fix/CS-40346-Uncaught-Exception-Error
fix: 🐛 handles the error in refreshtoken instead of throwing it
2 parents 074706a + 30fab7b commit e6580d9

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

.github/workflows/sast-scan.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

.jsdoc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"lib/stack/release/items/index.js",
2727
"lib/stack/label/index.js",
2828
"lib/stack/locale/index.js",
29+
"lib/stack/auditlog/index.js",
2930
"lib/stack/environment/index.js",
3031
"lib/stack/deliveryToken/index.js",
3132
"lib/stack/roles/index.js",

lib/core/concurrency-queue.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,18 @@ export function ConcurrencyQueue ({ axios, config }) {
9393
}
9494

9595
if (this.paused && request.retryCount > 0) {
96-
return new Promise(resolve => {
97-
this.unshift({ request, resolve })
96+
return new Promise((resolve, reject) => {
97+
this.unshift({ request, resolve, reject })
9898
})
9999
} else if (request.retryCount > 0) {
100100
return request
101101
}
102102

103-
return new Promise(resolve => {
103+
return new Promise((resolve, reject) => {
104104
request.onComplete = () => {
105-
this.running.pop({ request, resolve })
105+
this.running.pop({ request, resolve, reject })
106106
}
107-
this.push({ request, resolve })
107+
this.push({ request, resolve, reject })
108108
})
109109
}
110110

@@ -131,6 +131,7 @@ export function ConcurrencyQueue ({ axios, config }) {
131131
}, time))
132132
}
133133
}
134+
134135
const refreshToken = () => {
135136
return config.refreshToken().then((token) => {
136137
if (token.authorization) {
@@ -146,8 +147,19 @@ export function ConcurrencyQueue ({ axios, config }) {
146147
axios.httpClientParams.headers.authtoken = token.authtoken
147148
this.config.authtoken = token.authtoken
148149
}
149-
}).catch((error) => {
150-
throw error
150+
}).catch(() => {
151+
this.queue.forEach(queueItem => {
152+
queueItem.reject({
153+
errorCode: '401',
154+
errorMessage: 'Unable to refresh token',
155+
code: 'Unauthorized',
156+
message: 'Request failed with status code 401',
157+
name: 'Token Error',
158+
config: queueItem.request
159+
})
160+
})
161+
this.queue = []
162+
this.running = []
151163
}).finally(() => {
152164
this.queue.forEach((queueItem) => {
153165
if (this.config.authorization) {

lib/stack/locale/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ export function Locale (http, data = {}) {
2424
*
2525
* client.stack({ api_key: 'api_key'}).locale('locale_code').fetch()
2626
* .then((locale) => {
27-
* locale.title = 'My New Content Type'
28-
* locale.description = 'Content Type description'
29-
* return locale.update()
27+
* locale.fallback_locale = 'en-at'
28+
* return locale.update()
3029
* })
3130
* .then((locale) => console.log(locale))
3231
*

test/unit/concurrency-Queue-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ describe('Concurrency queue test', () => {
6969
if (req.url === '/user-session') {
7070
res.writeHead(200, { 'Content-Type': 'application/json' })
7171
res.end(JSON.stringify({ token }))
72+
73+
// Test for refresh token error - uncomment this
74+
// res.writeHead(401, { 'Content-Type': 'application/json' })
75+
// res.end(JSON.stringify({ errorCode: 401 }))
7276
} else if (req.url === '/unauthorized') {
7377
if (req.headers.authorization === token) {
7478
res.writeHead(200, { 'Content-Type': 'application/json' })

0 commit comments

Comments
 (0)