From afec5609521a62c3ac0d45a35599831aa1874fb3 Mon Sep 17 00:00:00 2001 From: Mihajlo Medjedovic Date: Mon, 14 Sep 2020 16:26:27 +0200 Subject: [PATCH 1/3] fix: JES API error handling if job does not exist --- src/SASViyaApiClient.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index b05e207..e9d4cf4 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -1001,9 +1001,21 @@ export class SASViyaApiClient { if (allJobsInFolder) { const jobSpec = allJobsInFolder.find((j: Job) => j.name === jobName) + + if (!jobSpec) { + console.error('Job was not found.') + throw new Error('Job was not found.') + } + const jobDefinitionLink = jobSpec?.links.find( (l) => l.rel === 'getResource' )?.href + + if (!jobDefinitionLink) { + console.error('Job definition URI was not found.') + throw new Error('Job definition URI was not found.') + } + const requestInfo: any = { method: 'GET' } From d61728e52a1c63dcff5034ad7c046dfc486b11bd Mon Sep 17 00:00:00 2001 From: Mihajlo Medjedovic Date: Thu, 17 Sep 2020 12:21:49 +0200 Subject: [PATCH 2/3] chore: removed extra console.error --- src/SASViyaApiClient.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 6d49e3b..9b65355 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -1003,7 +1003,6 @@ export class SASViyaApiClient { const jobSpec = allJobsInFolder.find((j: Job) => j.name === jobName) if (!jobSpec) { - console.error('Job was not found.') throw new Error('Job was not found.') } @@ -1012,7 +1011,6 @@ export class SASViyaApiClient { )?.href if (!jobDefinitionLink) { - console.error('Job definition URI was not found.') throw new Error('Job definition URI was not found.') } From 6a055a4fc677217a26bcd9eeaf935a443dde42a3 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Fri, 18 Sep 2020 12:54:17 +0300 Subject: [PATCH 3/3] fix(error): added error handling for http responses with status 401 and 403 --- src/SASViyaApiClient.ts | 19 ++++++------------- src/utils/makeRequest.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 9b65355..94a7dc0 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -323,17 +323,14 @@ export class SASViyaApiClient { { headers } - ).catch((e) => { - console.error(e) - - if (e && e.status === 404) { + ).catch((err) => { + if (err && err.status === 404) { throw new Error( `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 @@ -1388,12 +1385,8 @@ export class SASViyaApiClient { const { result: contexts } = await this.request<{ items: Context[] }>( `${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`, { headers } - ).catch((e) => { - console.error(e) - - throw new Error( - `An error occurred when fetching the context '${contextName}'.` - ) + ).catch((err) => { + throw err }) if (!contexts || !(contexts.items && contexts.items.length)) { diff --git a/src/utils/makeRequest.ts b/src/utils/makeRequest.ts index 30a1188..427fd4c 100644 --- a/src/utils/makeRequest.ts +++ b/src/utils/makeRequest.ts @@ -37,13 +37,28 @@ export async function makeRequest( ...request, headers: { ...request.headers, [tokenHeader]: token } } + return fetch(url, retryRequest).then((res) => { etag = res.headers.get('ETag') 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 { - const body = await response.text() + let body: any = await response.text() if (needsRetry(body)) { if (retryCount < retryLimit) { @@ -65,6 +80,18 @@ export async function makeRequest( } } + 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 }) } } else { @@ -104,5 +131,6 @@ export async function makeRequest( return responseTransformed } }) + return { result, etag } }