1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 09:24:35 +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:
"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;

View File

@@ -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<Job>(
`/compute/sessions/${executionSessionId}/jobs`,
@@ -445,7 +446,7 @@ export class SASViyaApiClient {
jobResult = await this.requestClient
.get<any>(resultLink, accessToken, 'text/plain')
.catch(async (e) => {
if (e && e.status === 404) {
if (e instanceof NotFoundError) {
if (logLink) {
log = await this.requestClient
.get<any>(`${logLink.href}/content?limit=10000`, accessToken)

View File

@@ -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<Session>(`/compute/sessions/${id}`, accessToken)
.then(() => {

View File

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

View File

@@ -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<T>(
@@ -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<T>(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}`

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