1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-07 20:40:05 +00:00

Compare commits

..

2 Commits

Author SHA1 Message Date
Yury Shkoda
939e6803e1 Merge pull request #99 from sasjs/issue-96
fix(error): added error handling for http responses with status 401 and 403
2020-09-18 13:30:11 +03:00
Yury Shkoda
6a055a4fc6 fix(error): added error handling for http responses with status 401 and 403 2020-09-18 12:54:17 +03:00
2 changed files with 35 additions and 14 deletions

View File

@@ -323,17 +323,14 @@ export class SASViyaApiClient {
{ {
headers headers
} }
).catch((e) => { ).catch((err) => {
console.error(e) if (err && err.status === 404) {
if (e && e.status === 404) {
throw new Error( throw new Error(
`The context '${contextName}' was not found on this server.` `The context '${contextName}' was not found on this server.`
) )
} }
throw new Error(
`An error occurred when fetching the context '${contextName}'.` throw err
)
}) })
// An If-Match header with the value of the last ETag for the context // An If-Match header with the value of the last ETag for the context
@@ -1388,12 +1385,8 @@ export class SASViyaApiClient {
const { result: contexts } = await this.request<{ items: Context[] }>( const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`, `${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`,
{ headers } { headers }
).catch((e) => { ).catch((err) => {
console.error(e) throw err
throw new Error(
`An error occurred when fetching the context '${contextName}'.`
)
}) })
if (!contexts || !(contexts.items && contexts.items.length)) { if (!contexts || !(contexts.items && contexts.items.length)) {

View File

@@ -37,13 +37,28 @@ export async function makeRequest<T>(
...request, ...request,
headers: { ...request.headers, [tokenHeader]: token } headers: { ...request.headers, [tokenHeader]: token }
} }
return fetch(url, retryRequest).then((res) => { return fetch(url, retryRequest).then((res) => {
etag = res.headers.get('ETag') etag = res.headers.get('ETag')
return responseTransform(res) return responseTransform(res)
}) })
} else {
let body: any = await response.text()
try {
body = JSON.parse(body)
body.message = `Forbidden. Check your permissions and user groups. ${
body.message || ''
}`
body = JSON.stringify(body)
} catch (_) {}
return Promise.reject({ status: response.status, body })
} }
} else { } else {
const body = await response.text() let body: any = await response.text()
if (needsRetry(body)) { if (needsRetry(body)) {
if (retryCount < retryLimit) { if (retryCount < retryLimit) {
@@ -65,6 +80,18 @@ export async function makeRequest<T>(
} }
} }
if (response.status === 401) {
try {
body = JSON.parse(body)
body.message = `Unauthorized request. Check your credentials(client, secret, access token). ${
body.message || ''
}`
body = JSON.stringify(body)
} catch (_) {}
}
return Promise.reject({ status: response.status, body }) return Promise.reject({ status: response.status, body })
} }
} else { } else {
@@ -104,5 +131,6 @@ export async function makeRequest<T>(
return responseTransformed return responseTransformed
} }
}) })
return { result, etag } return { result, etag }
} }