From 3c894c41479cce8e30f22df9a1e9f6fb0b373ebf Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Thu, 28 Jan 2021 19:25:23 +0000 Subject: [PATCH] fix(*): handled 404s, set correct accept headers --- sasjs-tests/src/testSuites/SasjsRequests.ts | 30 +++++++++------------ src/SASViyaApiClient.ts | 7 ++--- src/SessionManager.ts | 6 ----- src/job-execution/JesJobExecutor.ts | 2 +- src/request/RequestClient.ts | 6 ++++- src/types/NotFoundError.ts | 7 +++++ 6 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 src/types/NotFoundError.ts diff --git a/sasjs-tests/src/testSuites/SasjsRequests.ts b/sasjs-tests/src/testSuites/SasjsRequests.ts index d78eca4..a7f30ba 100644 --- a/sasjs-tests/src/testSuites/SasjsRequests.ts +++ b/sasjs-tests/src/testSuites/SasjsRequests.ts @@ -26,26 +26,20 @@ export const sasjsRequestTests = (adapter: SASjs): TestSuite => ({ description: "Should make an error and capture log, in the same time it is testing if debug override is working", test: async () => { - return new Promise(async (resolve, reject) => { - adapter - .request("common/makeErr", data, { debug: true }) - .then((res) => { - //no action here, this request must throw error - }) - .catch((err) => { - let sasRequests = adapter.getSasRequests(); - let makeErrRequest: any = - sasRequests.find((req) => - req.serviceLink.includes("makeErr") - ) || null; + return adapter + .request("common/makeErr", data, { debug: true }) + .catch((err) => { + let sasRequests = adapter.getSasRequests(); + let makeErrRequest: any = + sasRequests.find((req) => req.serviceLink.includes("makeErr")) || + null; - if (!makeErrRequest) return resolve(false); + if (!makeErrRequest) return false; - return resolve( - !!(makeErrRequest.logFile && makeErrRequest.logFile.length > 0) - ); - }); - }); + return !!( + makeErrRequest.logFile && makeErrRequest.logFile.length > 0 + ); + }); }, assertion: (response) => { return response; diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 8116269..a2a5f42 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -19,6 +19,7 @@ import { Logger, LogLevel } from '@sasjs/utils/logger' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { parseAndSubmitAuthorizeForm } from './auth' import { RequestClient } from './request/RequestClient' +import { NotFoundError } from './types/NotFoundError' /** * A client for interfacing with the SAS Viya REST API. @@ -367,13 +368,13 @@ export class SASViyaApiClient { } // Execute job in session - const jobRequestBody = JSON.stringify({ + const jobRequestBody = { name: fileName, description: 'Powered by SASjs', code: linesOfCode, variables: jobVariables, arguments: jobArguments - }) + } const { result: postedJob, etag } = await this.requestClient .post( `/compute/sessions/${executionSessionId}/jobs`, @@ -445,7 +446,7 @@ export class SASViyaApiClient { jobResult = await this.requestClient .get(resultLink, accessToken, 'text/plain') .catch(async (e) => { - if (e && e.status === 404) { + if (e instanceof NotFoundError) { if (logLink) { log = await this.requestClient .get(`${logLink.href}/content?limit=10000`, accessToken) diff --git a/src/SessionManager.ts b/src/SessionManager.ts index 7b5bfcd..cd907e5 100644 --- a/src/SessionManager.ts +++ b/src/SessionManager.ts @@ -22,7 +22,6 @@ export class SessionManager { private sessions: Session[] = [] private currentContext: Context | null = null - private csrfToken: CsrfToken | null = null private _debug: boolean = false private printedSessionState = { printed: false, @@ -59,11 +58,6 @@ export class SessionManager { } async clearSession(id: string, accessToken?: string) { - const deleteSessionRequest = { - method: 'DELETE', - headers: this.getHeaders(accessToken) - } - return await this.requestClient .delete(`/compute/sessions/${id}`, accessToken) .then(() => { diff --git a/src/job-execution/JesJobExecutor.ts b/src/job-execution/JesJobExecutor.ts index 786da0a..b93a915 100644 --- a/src/job-execution/JesJobExecutor.ts +++ b/src/job-execution/JesJobExecutor.ts @@ -28,7 +28,7 @@ export class JesJobExecutor implements JobExecutor { accessToken?: string ) { const loginCallback = loginRequiredCallback || (() => Promise.resolve()) - await this.sasViyaApiClient + return await this.sasViyaApiClient ?.executeJob(sasJob, config.contextName, config.debug, data, accessToken) .then((response) => { this.appendRequest(response, sasJob, config.debug) diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 0b80719..1fd9ea6 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -2,6 +2,7 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' import { CsrfToken, JobExecutionError } from '..' import { LoginRequiredError } from '../types' import { AuthorizeError } from '../types/AuthorizeError' +import { NotFoundError } from '../types/NotFoundError' export interface HttpClient { get( @@ -64,7 +65,6 @@ export class RequestClient implements HttpClient { withCredentials: true } if (contentType === 'text/plain') { - requestConfig.headers.Accept = '*/*' requestConfig.transformResponse = undefined } @@ -84,6 +84,8 @@ export class RequestClient implements HttpClient { return this.get(url, accessToken, contentType, overrideHeaders) } throw e + } else if (response?.status === 404) { + throw new NotFoundError(url) } throw e } @@ -272,6 +274,8 @@ export class RequestClient implements HttpClient { if (contentType === 'text/plain') { headers.Accept = '*/*' + } else { + headers.Accept = 'application/json' } if (accessToken) { headers.Authorization = `Bearer ${accessToken}` diff --git a/src/types/NotFoundError.ts b/src/types/NotFoundError.ts new file mode 100644 index 0000000..7455221 --- /dev/null +++ b/src/types/NotFoundError.ts @@ -0,0 +1,7 @@ +export class NotFoundError extends Error { + constructor(public url: string) { + super(`Error: Resource at ${url} was not found`) + this.name = 'NotFoundError' + Object.setPrototypeOf(this, NotFoundError.prototype) + } +}