1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 09:24:35 +00:00

Compare commits

...

8 Commits

Author SHA1 Message Date
Yury Shkoda
e0140a23c2 Merge pull request #264 from sasjs/error-handling
fix(RequestClient): improved handleError method
2021-02-25 09:52:40 +03:00
Yury Shkoda
ff6698a9d1 chore: removed async from parseResponse method 2021-02-25 09:51:00 +03:00
Yury Shkoda
843d498b72 fix(RequestClient): improved handleError method 2021-02-25 09:14:49 +03:00
Yury Shkoda
f679b17cbe Merge pull request #263 from sasjs/error-handling
fix(docs): updated docs
2021-02-24 15:44:11 +03:00
Yury Shkoda
36a0f0e743 Merge branch 'master' into error-handling 2021-02-24 15:39:40 +03:00
Yury Shkoda
ad563b9bc8 fix(docs): updated docs 2021-02-24 15:37:31 +03:00
Yury Shkoda
893cce7f21 Merge pull request #262 from sasjs/error-handling
fix(startComputeJob): improved error handling
2021-02-24 14:50:15 +03:00
Yury Shkoda
bf7e8fd0e6 chore(startComputeJob): improved error handling 2021-02-24 12:49:41 +03:00
50 changed files with 222 additions and 166 deletions

4
.gitignore vendored
View File

@@ -3,4 +3,6 @@ build
.env
/coverage
/coverage
.DS_Store

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -21,6 +21,7 @@ import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
import { RequestClient } from './request/RequestClient'
import { NotFoundError } from './types/NotFoundError'
import { SasAuthResponse } from '@sasjs/utils/types'
import { prefixMessage } from '@sasjs/utils/error'
/**
* A client for interfacing with the SAS Viya REST API.
@@ -280,25 +281,24 @@ export class SASViyaApiClient {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
if (accessToken) headers.Authorization = `Bearer ${accessToken}`
let executionSessionId: string
const session = await this.sessionManager
.getSession(accessToken)
.catch((err) => {
throw err
throw prefixMessage(err, 'Error while getting session. ')
})
executionSessionId = session!.id
if (printPid) {
const { result: jobIdVariable } = await this.sessionManager.getVariable(
executionSessionId,
'SYSJOBID',
accessToken
)
const { result: jobIdVariable } = await this.sessionManager
.getVariable(executionSessionId, 'SYSJOBID', accessToken)
.catch((err) => {
throw prefixMessage(err, 'Error while getting session variable. ')
})
if (jobIdVariable && jobIdVariable.value) {
const relativeJobPath = this.rootFolderName
@@ -331,6 +331,7 @@ export class SASViyaApiClient {
}
let fileName
if (isRelativePath(jobPath)) {
fileName = `exec-${
jobPath.includes('/') ? jobPath.split('/')[1] : jobPath
@@ -352,7 +353,7 @@ export class SASViyaApiClient {
if (data) {
if (JSON.stringify(data).includes(';')) {
files = await this.uploadTables(data, accessToken).catch((err) => {
throw err
throw prefixMessage(err, 'Error while uploading tables. ')
})
jobVariables['_webin_file_count'] = files.length
@@ -376,19 +377,18 @@ export class SASViyaApiClient {
variables: jobVariables,
arguments: jobArguments
}
const { result: postedJob, etag } = await this.requestClient
.post<Job>(
`/compute/sessions/${executionSessionId}/jobs`,
jobRequestBody,
accessToken
)
.catch((err: any) => {
throw err
.catch((err) => {
throw prefixMessage(err, 'Error while posting job. ')
})
if (!waitForResult) {
return session
}
if (!waitForResult) return session
if (debug) {
console.log(`Job has been submitted for '${fileName}'.`)
@@ -404,7 +404,9 @@ export class SASViyaApiClient {
etag,
accessToken,
pollOptions
)
).catch((err) => {
throw prefixMessage(err, 'Error while polling job status. ')
})
const { result: currentJob } = await this.requestClient
.get<Job>(
@@ -412,7 +414,7 @@ export class SASViyaApiClient {
accessToken
)
.catch((err) => {
throw err
throw prefixMessage(err, 'Error while getting job. ')
})
let jobResult
@@ -427,7 +429,7 @@ export class SASViyaApiClient {
res.result.items.map((i: any) => i.line).join('\n')
)
.catch((err) => {
throw err
throw prefixMessage(err, 'Error while getting log. ')
})
}
@@ -455,7 +457,7 @@ export class SASViyaApiClient {
res.result.items.map((i: any) => i.line).join('\n')
)
.catch((err) => {
throw err
throw prefixMessage(err, 'Error while getting log. ')
})
return Promise.reject({
@@ -464,6 +466,7 @@ export class SASViyaApiClient {
})
}
}
return {
result: JSON.stringify(e)
}
@@ -473,7 +476,7 @@ export class SASViyaApiClient {
await this.sessionManager
.clearSession(executionSessionId, accessToken)
.catch((err) => {
throw err
throw prefixMessage(err, 'Error while clearing session. ')
})
return { result: jobResult?.result, log }
@@ -490,7 +493,7 @@ export class SASViyaApiClient {
true
)
} else {
throw e
throw prefixMessage(e, 'Error while executing script. ')
}
}
}
@@ -814,9 +817,12 @@ export class SASViyaApiClient {
? `${this.rootFolderName}/${folderPath}`
: folderPath
await this.populateFolderMap(fullFolderPath, accessToken)
await this.populateFolderMap(fullFolderPath, accessToken).catch((err) => {
throw prefixMessage(err, 'Error while populating folder map. ')
})
const jobFolder = this.folderMap.get(fullFolderPath)
if (!jobFolder) {
throw new Error(
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'`
@@ -824,6 +830,7 @@ export class SASViyaApiClient {
}
const headers: any = { 'Content-Type': 'application/json' }
if (!!accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
@@ -847,10 +854,14 @@ export class SASViyaApiClient {
const {
result: jobDefinition
} = await this.requestClient.get<JobDefinition>(
`${this.serverUrl}${jobDefinitionLink.href}`,
accessToken
)
} = await this.requestClient
.get<JobDefinition>(
`${this.serverUrl}${jobDefinitionLink.href}`,
accessToken
)
.catch((err) => {
throw prefixMessage(err, 'Error while getting job definition. ')
})
code = jobDefinition.code
@@ -861,6 +872,7 @@ export class SASViyaApiClient {
if (!code) code = ''
const linesToExecute = code.replace(/\r\n/g, '\n').split('\n')
return await this.executeScript(
sasJob,
linesToExecute,
@@ -872,7 +884,9 @@ export class SASViyaApiClient {
waitForResult,
pollOptions,
printPid
)
).catch((err) => {
throw prefixMessage(err, 'Error while executing script. ')
})
}
/**
@@ -1007,19 +1021,27 @@ export class SASViyaApiClient {
}
const url = '/folders/folders/@item?path=' + path
const { result: folder } = await this.requestClient.get<Folder>(
`${url}`,
accessToken
)
const { result: folder } = await this.requestClient
.get<Folder>(`${url}`, accessToken)
.catch((err) => {
throw prefixMessage(err, 'Error while getting folder. ')
})
if (!folder) {
throw new Error(`The path ${path} does not exist on ${this.serverUrl}`)
}
const { result: members } = await this.requestClient.get<{ items: any[] }>(
`/folders/folders/${folder.id}/members?limit=${folder.memberCount}`,
accessToken
)
const { result: members } = await this.requestClient
.get<{ items: any[] }>(
`/folders/folders/${folder.id}/members?limit=${folder.memberCount}`,
accessToken
)
.catch((err) => {
throw prefixMessage(err, 'Error while getting members. ')
})
const itemsAtRoot = members.items
this.folderMap.set(path, itemsAtRoot)
}
@@ -1052,11 +1074,15 @@ export class SASViyaApiClient {
Promise.reject(`Job state link was not found.`)
}
const { result: state } = await this.requestClient.get<string>(
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
accessToken,
'text/plain'
)
const { result: state } = await this.requestClient
.get<string>(
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
accessToken,
'text/plain'
)
.catch((err) => {
throw prefixMessage(err, 'Error while getting job state. ')
})
const currentState = state.trim()
if (currentState === 'completed') {
@@ -1073,11 +1099,15 @@ export class SASViyaApiClient {
postedJobState === 'pending'
) {
if (stateLink) {
const { result: jobState } = await this.requestClient.get<string>(
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
accessToken,
'text/plain'
)
const { result: jobState } = await this.requestClient
.get<string>(
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
accessToken,
'text/plain'
)
.catch((err) => {
throw prefixMessage(err, 'Error while getting job state. ')
})
postedJobState = jobState.trim()
@@ -1119,11 +1149,11 @@ export class SASViyaApiClient {
)
}
const uploadResponse = await this.requestClient.uploadFile(
`${this.serverUrl}/files/files#rawUpload`,
csv,
accessToken
)
const uploadResponse = await this.requestClient
.uploadFile(`${this.serverUrl}/files/files#rawUpload`, csv, accessToken)
.catch((err) => {
throw prefixMessage(err, 'Error while uploading file. ')
})
uploadedFiles.push({ tableName, file: uploadResponse.result })
}

View File

@@ -64,7 +64,7 @@ export class SessionManager {
this.sessions = this.sessions.filter((s) => s.id !== id)
})
.catch((err) => {
throw err
throw prefixMessage(err, 'Error while deleting session. ')
})
}

View File

@@ -5,6 +5,7 @@ import { LoginRequiredError } from '../types'
import { AuthorizeError } from '../types/AuthorizeError'
import { NotFoundError } from '../types/NotFoundError'
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { prefixMessage } from '@sasjs/utils/error'
export interface HttpClient {
get<T>(
@@ -97,7 +98,9 @@ export class RequestClient implements HttpClient {
.catch(async (e) => {
return await this.handleError(e, () =>
this.get<T>(url, accessToken, contentType, overrideHeaders)
)
).catch((err) => {
throw prefixMessage(err, 'Error while handling error. ')
})
})
}
@@ -331,33 +334,54 @@ export class RequestClient implements HttpClient {
private handleError = async (e: any, callback: any) => {
const response = e.response as AxiosResponse
if (e instanceof AuthorizeError) {
const res = await this.httpClient.get(e.confirmUrl, {
responseType: 'text',
headers: { 'Content-Type': 'text/plain', Accept: '*/*' }
})
const res = await this.httpClient
.get(e.confirmUrl, {
responseType: 'text',
headers: { 'Content-Type': 'text/plain', Accept: '*/*' }
})
.catch((err) => {
throw prefixMessage(err, 'Error while getting error confirmUrl. ')
})
if (isAuthorizeFormRequired(res?.data as string)) {
await this.authorize(res.data as string)
}
return await callback()
return await callback().catch((err: any) => {
throw prefixMessage(
err,
'Error while executing callback in handleError. '
)
})
}
if (e instanceof LoginRequiredError) {
this.clearCsrfTokens()
}
if (response?.status === 403 || response?.status === 449) {
this.parseAndSetCsrfToken(response)
if (this.csrfToken.headerName && this.csrfToken.value) {
return await callback()
return await callback().catch((err: any) => {
throw prefixMessage(
err,
'Error while executing callback in handleError. '
)
})
}
throw e
} else if (response?.status === 404) {
throw new NotFoundError(response.config.url!)
}
throw e
}
private async parseResponse<T>(response: AxiosResponse<any>) {
private parseResponse<T>(response: AxiosResponse<any>) {
const etag = response?.headers ? response.headers['etag'] : ''
let parsedResponse