1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-07 12:30:06 +00:00

fix(*): handled 404s, set correct accept headers

This commit is contained in:
Krishna Acondy
2021-01-28 19:25:23 +00:00
parent 23d151c919
commit 3c894c4147
6 changed files with 29 additions and 29 deletions

View File

@@ -26,26 +26,20 @@ export const sasjsRequestTests = (adapter: SASjs): TestSuite => ({
description: description:
"Should make an error and capture log, in the same time it is testing if debug override is working", "Should make an error and capture log, in the same time it is testing if debug override is working",
test: async () => { test: async () => {
return new Promise(async (resolve, reject) => { return adapter
adapter
.request("common/makeErr", data, { debug: true }) .request("common/makeErr", data, { debug: true })
.then((res) => {
//no action here, this request must throw error
})
.catch((err) => { .catch((err) => {
let sasRequests = adapter.getSasRequests(); let sasRequests = adapter.getSasRequests();
let makeErrRequest: any = let makeErrRequest: any =
sasRequests.find((req) => sasRequests.find((req) => req.serviceLink.includes("makeErr")) ||
req.serviceLink.includes("makeErr") null;
) || null;
if (!makeErrRequest) return resolve(false); if (!makeErrRequest) return false;
return resolve( return !!(
!!(makeErrRequest.logFile && makeErrRequest.logFile.length > 0) makeErrRequest.logFile && makeErrRequest.logFile.length > 0
); );
}); });
});
}, },
assertion: (response) => { assertion: (response) => {
return response; return response;

View File

@@ -19,6 +19,7 @@ import { Logger, LogLevel } from '@sasjs/utils/logger'
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
import { parseAndSubmitAuthorizeForm } from './auth' import { parseAndSubmitAuthorizeForm } from './auth'
import { RequestClient } from './request/RequestClient' import { RequestClient } from './request/RequestClient'
import { NotFoundError } from './types/NotFoundError'
/** /**
* A client for interfacing with the SAS Viya REST API. * A client for interfacing with the SAS Viya REST API.
@@ -367,13 +368,13 @@ export class SASViyaApiClient {
} }
// Execute job in session // Execute job in session
const jobRequestBody = JSON.stringify({ const jobRequestBody = {
name: fileName, name: fileName,
description: 'Powered by SASjs', description: 'Powered by SASjs',
code: linesOfCode, code: linesOfCode,
variables: jobVariables, variables: jobVariables,
arguments: jobArguments arguments: jobArguments
}) }
const { result: postedJob, etag } = await this.requestClient const { result: postedJob, etag } = await this.requestClient
.post<Job>( .post<Job>(
`/compute/sessions/${executionSessionId}/jobs`, `/compute/sessions/${executionSessionId}/jobs`,
@@ -445,7 +446,7 @@ export class SASViyaApiClient {
jobResult = await this.requestClient jobResult = await this.requestClient
.get<any>(resultLink, accessToken, 'text/plain') .get<any>(resultLink, accessToken, 'text/plain')
.catch(async (e) => { .catch(async (e) => {
if (e && e.status === 404) { if (e instanceof NotFoundError) {
if (logLink) { if (logLink) {
log = await this.requestClient log = await this.requestClient
.get<any>(`${logLink.href}/content?limit=10000`, accessToken) .get<any>(`${logLink.href}/content?limit=10000`, accessToken)

View File

@@ -22,7 +22,6 @@ export class SessionManager {
private sessions: Session[] = [] private sessions: Session[] = []
private currentContext: Context | null = null private currentContext: Context | null = null
private csrfToken: CsrfToken | null = null
private _debug: boolean = false private _debug: boolean = false
private printedSessionState = { private printedSessionState = {
printed: false, printed: false,
@@ -59,11 +58,6 @@ export class SessionManager {
} }
async clearSession(id: string, accessToken?: string) { async clearSession(id: string, accessToken?: string) {
const deleteSessionRequest = {
method: 'DELETE',
headers: this.getHeaders(accessToken)
}
return await this.requestClient return await this.requestClient
.delete<Session>(`/compute/sessions/${id}`, accessToken) .delete<Session>(`/compute/sessions/${id}`, accessToken)
.then(() => { .then(() => {

View File

@@ -28,7 +28,7 @@ export class JesJobExecutor implements JobExecutor {
accessToken?: string accessToken?: string
) { ) {
const loginCallback = loginRequiredCallback || (() => Promise.resolve()) const loginCallback = loginRequiredCallback || (() => Promise.resolve())
await this.sasViyaApiClient return await this.sasViyaApiClient
?.executeJob(sasJob, config.contextName, config.debug, data, accessToken) ?.executeJob(sasJob, config.contextName, config.debug, data, accessToken)
.then((response) => { .then((response) => {
this.appendRequest(response, sasJob, config.debug) this.appendRequest(response, sasJob, config.debug)

View File

@@ -2,6 +2,7 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import { CsrfToken, JobExecutionError } from '..' import { CsrfToken, JobExecutionError } from '..'
import { LoginRequiredError } from '../types' import { LoginRequiredError } from '../types'
import { AuthorizeError } from '../types/AuthorizeError' import { AuthorizeError } from '../types/AuthorizeError'
import { NotFoundError } from '../types/NotFoundError'
export interface HttpClient { export interface HttpClient {
get<T>( get<T>(
@@ -64,7 +65,6 @@ export class RequestClient implements HttpClient {
withCredentials: true withCredentials: true
} }
if (contentType === 'text/plain') { if (contentType === 'text/plain') {
requestConfig.headers.Accept = '*/*'
requestConfig.transformResponse = undefined requestConfig.transformResponse = undefined
} }
@@ -84,6 +84,8 @@ export class RequestClient implements HttpClient {
return this.get<T>(url, accessToken, contentType, overrideHeaders) return this.get<T>(url, accessToken, contentType, overrideHeaders)
} }
throw e throw e
} else if (response?.status === 404) {
throw new NotFoundError(url)
} }
throw e throw e
} }
@@ -272,6 +274,8 @@ export class RequestClient implements HttpClient {
if (contentType === 'text/plain') { if (contentType === 'text/plain') {
headers.Accept = '*/*' headers.Accept = '*/*'
} else {
headers.Accept = 'application/json'
} }
if (accessToken) { if (accessToken) {
headers.Authorization = `Bearer ${accessToken}` headers.Authorization = `Bearer ${accessToken}`

View File

@@ -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)
}
}